【问题标题】:Find the size of a string pointed by a pointer查找指针指向的字符串的大小
【发布时间】:2012-11-13 02:43:15
【问题描述】:
#include <stdio.h>

int main ()
{
    char *ptr = "stackoverflow"

}

有没有办法找到ptr指向的stackoverflow的长度,因为sizeof ptr总是给出4

【问题讨论】:

  • sizeof "stackoverflow" 产生正确的 14 值。

标签: c


【解决方案1】:

使用strlen 查找字符串的长度(字符数)

const char *ptr = "stackoverflow";
size_t length = strlen(ptr);

另一个小问题,请注意ptr 是一个字符串字面量(指向无法修改的常量内存的指针)。将其声明为 const 以显示这一点是更好的做法。

【讨论】:

  • +1 表示两个好的做法,将字符串文字分配给 const char * 和使用 size_t
  • 确实 - 请注意,如果目标不是以 null 结尾的字符串,这将一直测量,直到它在内存中随机找到 null,或者在搜索过程中触发内存保护错误。当然(正如其他人指出的那样)返回值不包括终止 null 的大小。
【解决方案2】:

尽管这是一个通用的 C 问题,但在为 C++ 查找此问题时,它的命中率很高。我不仅在 C/C++ 领域,我还必须注意 Microsoft 的 Security Development Lifecycle (SDL) Banned Function Calls 用于一个特定项目,这使得 strlen 无法通过,

对于关键应用程序,例如接受匿名 Internet 连接的应用程序, strlen 也必须替换...

无论如何,这个答案基本上只是对其他答案的一个转折,但已获得批准的 Microsoft C++ 替代函数调用以及针对 65,535 字节的C99's updated limit 的宽字符处理的注意事项。

#include <iostream>
#include <Windows.h>
int wmain()
{
    //  1 byte per char, 65535 byte limit per C99 updated standard
    //  https://stackoverflow.com/a/5351964/3543437
    const size_t ASCII_ARRAY_SAFE_SIZE_LIMIT = 65535; 

    //  Theoretical UTF-8 upper byte limit of 6; can typically use 16383 for 4 bytes per char instead:
    //  https://stijndewitt.com/2014/08/09/max-bytes-in-a-utf-8-char/
    const size_t UNICODE_ARRAY_SAFE_SIZE_LIMIT = 10922;

    char ascii_array[] = "ACSCII stuff like ABCD1234.";
    wchar_t unicode_array[] = L"Unicode stuff like → ∞ ∑ Σὲ γνωρίζω τὴν ደሀ ᚦᚫᛏ.";

    char * ascii_array_ptr = &ascii_array[0];
    wchar_t * unicode_array_ptr = &unicode_array[0];

    std::cout << "The string length of the char array is: " << strnlen_s(ascii_array_ptr, ASCII_ARRAY_SAFE_SIZE_LIMIT) << std::endl;
    std::wcout << L"The string length of the wchar_t array is: " << wcsnlen_s(unicode_array_ptr, UNICODE_ARRAY_SAFE_SIZE_LIMIT) << std::endl;

    return 0;
}

输出:

The string length of the char array is: 27
The string length of the wchar_t array is: 47

【讨论】:

    【解决方案3】:

    纯粹使用指针可以使用指针运算:

    int strLen(char *s)
    {
        int *p = s;
        while(*p !=’\0’)
        {
            p++;  /* increase the address until the end */
        }
        Return p – s; /* Subtract the two addresses, end - start */
    }
    

    【讨论】:

    • 这不仅适用于以空字符结尾的字符串吗?我想如果你知道这就是你正在使用的全部内容,那么可以使用它......
    【解决方案4】:
    #include<stdio.h>
    main()
    {
        int mystrlen(char *);
        char str[100];
        char *p;
        p=str;
        printf("Enter the string..?\n");
        scanf("%s",p);
        int x=mystrlen(p);
        printf("Length of string is=%d\n",x);
    
    
    }
    int mystrlen(char *p)
    {
        int c=0;
        while(*p!='\0')
        {
            c++;
            *p++;
        }
        return(c);
    }
    

    简单易懂的代码

    【讨论】:

    • 这是迄今为止最好的答案,而不是仅仅指出一个内置方法。
    • 你可以在 while 循环中使用 *p 而不是 *p!='\0'。
    • 我知道这是一个旧答案,但是如果 p 指向一个字符而不是字符串文字会发生什么?我用标准的strlen 函数对此进行了测试,它似乎返回了废话。
    【解决方案5】:

    string.h 提供的strlen() 函数告诉您参数指向的字符串包含多少个“真实字符”。但是,这个长度不包括终止空字符'\0';如果您需要分配内存的长度,则必须考虑它。

    这 4 个字节是您平台上指向 char 的指针的大小。

    【讨论】:

      【解决方案6】:

      如果 ptr 长度是函数的参数,则将指针用作字符串是合理的。我们可以通过以下代码获取字符串长度:

      char *ptr = "stackoverflow";
      length=strlen((const char *)ptr);
      

      为了进一步解释,如果字符串是用户输入的可变长度字符串,我们可以使用以下代码:

      unsigned char *ptr;
      ptr=(unsigned char *)calloc(50, sizeof(unsigned char));
      scanf("%s",ptr );
      length=strlen((const char *)ptr);
      

      【讨论】:

        【解决方案7】:
        1. sizeof() 返回类型所需的大小。由于在这种情况下传递给 sizeof 的类型是指针,因此它将返回指针的大小。

          如果您需要指针所指向的数据大小,则必须通过storing it explicitly 记住它。

        2. sizeof() 在编译时工作。所以,sizeof(ptr) 通常是return 4 or 8 bytes。而是使用strlen

        【讨论】:

          【解决方案8】:

          您可以尝试使用:

          char *ptr = "stackoverflow"
          size_t len = strlen(ptr);
          

          【讨论】:

            【解决方案9】:

            您正在寻找strlen() 函数。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 2013-01-25
              • 2013-04-26
              • 1970-01-01
              • 2011-04-12
              • 1970-01-01
              • 2021-06-28
              • 2021-12-16
              相关资源
              最近更新 更多