为什么 C 数组在传递给函数时有错误的 sizeof() 值?
完整的示例:
#include <stdio.h>
void test(int arr[]) {
int arrSize = (int)(sizeof(arr) / sizeof(arr[0]));
printf("%d\n", arrSize); // 2 (wrong?!)
}
int main (int argc, const char * argv[]) {
int point[3] = {50, 30, 12};
int arrSize = (int)(sizeof(point) / sizeof(point[0]));
printf("%d\n", arrSize); // 3 (correct :-) )
test(point);
return 0;
}
在将其传递给函数之前,sizeof 为我提供了正确的值。在函数中的完全相同的数组上执行完全相同的操作会产生奇怪的结果。缺少一个元素。为什么?
Complete example:
#include <stdio.h>
void test(int arr[]) {
int arrSize = (int)(sizeof(arr) / sizeof(arr[0]));
printf("%d\n", arrSize); // 2 (wrong?!)
}
int main (int argc, const char * argv[]) {
int point[3] = {50, 30, 12};
int arrSize = (int)(sizeof(point) / sizeof(point[0]));
printf("%d\n", arrSize); // 3 (correct :-) )
test(point);
return 0;
}
Before passing it to a function, sizeof gives me the correct value. Doing the exact same thing on the exact same array in the function gives weird results. There's one element missing. Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
当您将数组传递给 C 中的函数时,数组会衰减为指向其第一个元素的指针。当您在参数上使用
sizeof
时,您将获取指针的大小,而不是数组本身。如果您需要函数知道数组的大小,则应将其作为单独的参数传递:
另请注意,出于类似的原因(采用
sizeof
指针),sizeof (point)/sizeof(point[0])
技巧不适用于动态分配的数组,仅适用于在堆栈上分配的数组。When you pass an array into a function in C, the array decays into a pointer to its first element. When you use
sizeof
on the parameter, you are taking the size of the pointer, not the array itself.If you need the function to know the size of the array, you should pass it as a separate parameter:
Also note that, for a similar reason (taking the
sizeof
a pointer), thesizeof(point)/sizeof(point[0])
trick doesn't work for a dynamically allocated array, only an array allocated on the stack.因为数组衰减为指针作为函数参数传递,因此对于 32 位和 64 位平台,
sizeof
分别为您提供 4 和 8。Because array is decayed to a pointer when passed as function argument, so
sizeof
gives you 4 and 8 for 32- and 64-bit platforms respectively.另外,了解
sizeof
是在编译时评估的也很重要。既然如此,根据传入的内容在test()
中期望不同的输出是没有意义的。sizeof
计算是在函数编译时完成的。Also, it's important to understand that
sizeof
is evaluated at compile time. Since that's the case, it doesn't make sense to expect different output intest()
depending on what was passed in. Thesizeof
calculation was done when the function was compiled.因为,当它被传递时,实际上只传递了指向数组的指针。
您的问题也在C 编程常见问题解答中得到解答。问题 6.21。
Because, when it's passed, only the pointer to array is actually being passed.
Your question is also answered at The C Programming FAQ. Question 6.21.
因为在 C、C++ 和 Objective-C 中,函数实际上不能有数组参数。它们只能具有看起来像数组参数的参数,但它们不是。在您的示例中,
编译器看到“有一个看起来像 int 数组的参数”,并用“指向 int 的指针”替换该参数。因此,您编写的函数绝对是百分百相同的,
因此,在函数内 sizeof (arr) 将为您提供“指向 int 的指针”的大小。
Because in C, C++, and Objective-C, functions cannot actually have array parameters. They only can have parameters that look like array parameters, but they aren't. In your example,
the compiler sees "there is a parameter that looks like an array of int", and it replaces that parameter with a "pointer to int". So the function that you wrote is absolutely, one hundred percent, identical to
Therefore, inside the function sizeof (arr) will give you the size of a "pointer to int".
因为 sizeof() 不会告诉您 C 中数组的大小。它执行完全不同的操作。
sizeof C++ 参考
Because sizeof() does NOT tell you the size of an array in C. It does something completely different.
sizeof C++ reference