这可能吗? [指向字符数组C的指针]

发布于 2024-11-05 05:05:29 字数 238 浏览 1 评论 0原文

这可能吗?

size_t calculate(char *s)
{
    // I would like to return 64
}

int main()
{
    char s[64];

    printf("%d", calculate(s));

    return 0;
}

我想编写一个函数来计算 main() 中声明的 char 数组的大小。

Is this possible?

size_t calculate(char *s)
{
    // I would like to return 64
}

int main()
{
    char s[64];

    printf("%d", calculate(s));

    return 0;
}

I want to write a function which calculates the size of the char array declared in main().

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

凉城凉梦凉人心 2024-11-12 05:05:29

仅给出指针参数 s 的函数 calculate() 无法计算数组有多大。数组的大小未编码在指针中,也未从指针访问。如果它被设计为以空结尾的字符串作为参数,它可以确定该字符串的长度;当然,这就是 strlen() 的作用。但是,如果它想知道可以安全地将多少信息复制到数组中,则必须告诉它数组有多大,或者假设有足够的空间。

正如其他人指出的那样,可以在数组定义可见的函数中使用 sizeof() 运算符来获取数组的大小。但在无法看到数组定义的函数中,您无法有效地应用 sizeof() 运算符。如果数组是一个全局变量,其定义(而不是声明)位于编写 calculate() 的范围(可见)内 - 因此,不是函数的参数 - 则 calculate( ) 可以表示大小。

这就是为什么很多很多 C 函数需要一个指针和一个长度。由于缺乏这些信息,C 语言很容易被人们误用并产生“缓冲区溢出”错误,即代码试图将一加仑的信息装入一品脱罐中。

Your function calculate(), given just the pointer argument s, cannot calculate how big the array is. The size of the array is not encoded in the pointer, or accessible from the pointer. If it is designed to take a null-terminated string as an argument, it can determine how long that string is; that's what strlen() does, of course. But if it wants to know how much information it can safely copy into the array, it has to be told how big the array is, or make an assumption that there is enough space.

As others have pointed out, the sizeof() operator can be used in the function where the array definition is visible to get the size of the array. But in a function that cannot see the definition of the array you cannot usefully apply the sizeof() operator. If the array was a global variable whose definition (not declaration) was in scope (visible) where calculate() was written - and not, therefore, the parameter to the function - then calculate() could indicate the size.

This is why many, many C functions take a pointer and a length. The absence of the information is why C is somewhat prone to people misusing it and producing 'buffer overflow' bugs, where the code tries to fit a gallon of information into a pint pot.

メ斷腸人バ 2024-11-12 05:05:29

在静态声明的 char[] 上,您可以使用运算符 sizeof,在本例中它将返回 64。

printf("%d", sizeof(s));

在动态声明的 char* 上,无法获取已分配内存的大小。

动态数组是通过malloc等获得的。所有其他都是静态声明,并且您可以对它们使用sizeof,只要您在声明数组的相同范围内使用它(相同的函数,在您的例)。

On statically declared char[] you can use operator sizeof, which will return 64 in this case.

printf("%d", sizeof(s));

On dynamically declared char*, it is not possible to get the size of the allocated memory.

Dynamic arrays are obtained through malloc and friends. All the others are statically declared, and you can use sizeof on them, as long as you use it in the same scope as the array was declared (same function, in your case, for example).

如痴如狂 2024-11-12 05:05:29

是的,如果 s 在其数组末尾有一个特定字符,这是可能的。例如,您可以有 s[63] = 125 并且知道从 0 到 62 的所有其他字符都不会是 125,您可以执行 for 循环,直到找到 125 并返回数组。

否则,这是不可能的,因为函数参数中的 s 只是指向数组的指针,因此 calculate 中的 sizeof(s) 只会返回您的机器指针大小,而不是像某些人预期的那样 64。

Yes, it's possible if s has a specific character in the end of it's array. For example you could have s[63] = 125 and by knowing that every other character from 0 to 62 won't be 125, you can do a for loop until you find 125 and return the size of the array.

Otherwise, it's not possible, as s in the function parameter is just a pointer to your array, so sizeof(s) inside calculate will only return your machines pointer size and not 64 as someone could expected.

山川志 2024-11-12 05:05:29

不幸的是,您无法仅根据指针值确定相应数组中有多少元素。您要么需要数组中某种哨兵值(例如用于字符串的 0 终止符),要么需要单独跟踪它。

可以做的是使用sizeof运算符获取数组中的字节数或元素数:

char arr[64];

size_t size = sizeof arr;                 // # of bytes in arr
size_t count = sizeof arr / sizeof *arr;  // # of elements in arr

但是,这仅在arr是一个数组类型;如果您尝试在函数中执行此操作,

 size_t calculate(char *s)
 {
   return sizeof s;
 }

它将返回指针值的字节大小,而不是相应的数组对象的大小。

Unfortunately, you cannot determine from a pointer value alone how many elements are in the corresponding array. You either need some sort of sentinel value in the array (like the 0 terminator used for strings), or you need to keep track of it separately.

What you can do is get the number of bytes or elements in an array using the sizeof operator:

char arr[64];

size_t size = sizeof arr;                 // # of bytes in arr
size_t count = sizeof arr / sizeof *arr;  // # of elements in arr

However, this only works if arr is an array type; if you tried to do this in your function

 size_t calculate(char *s)
 {
   return sizeof s;
 }

it would return the size in bytes of the pointer value, not of the corresponding array object.

下雨或天晴 2024-11-12 05:05:29

不会。char *xchar x[] 只是创建一个指向内存位置的指针。指针不保存有关内存区域大小的任何信息。

但是,char *x = "Hello" 占用 6 个字节(包括终止 null),strlen(x) 将返回 5。这依赖于 null 字符在字符串末尾,strlen 仍然对底层缓冲区一无所知。所以 strlen("Hello\000There") 仍然是 5。

No. char *x or char x[] just creates a pointer to a memory location. A pointer doesn't hold any information about the size of the memory region.

However, char *x = "Hello" occupies 6 bytes (including the terminating null), and strlen(x) would return 5. This relies on the null char at the end of the string, strlen still knows nothing about the underlying buffer. So strlen("Hello\000There") would still be 5.

绿光 2024-11-12 05:05:29

这通常是用 C 中的宏来完成的,例如:

#define ARRAY_SIZE(x) (sizeof(x)/sizeof(*x))

这是否是一个好主意是一个完全不同的问题。

This is usually done with a macro in C, like:

#define ARRAY_SIZE(x) (sizeof(x)/sizeof(*x))

Whether it's a good idea is a totally different question.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文