为什么 C 数组在传递给函数时有错误的 sizeof() 值?

发布于 2024-09-03 08:57:14 字数 489 浏览 12 评论 0原文

完整的示例:

#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 技术交流群。

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

发布评论

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

评论(6

囚你心 2024-09-10 08:57:14

当您将数组传递给 C 中的函数时,数组会衰减为指向其第一个元素的指针。当您在参数上使用 sizeof 时,您将获取指针的大小,而不是数组本身。

如果您需要函数知道数组的大小,则应将其作为单独的参数传递:

void test(int arr[], size_t elems) {
   /* ... */
}

int main(int argc, const char * argv[]) {
   int point[3] = {50, 30, 12};
   /* ... */
   test(point, sizeof(point)/sizeof(point[0]));
   /* ... */
}

另请注意,出于类似的原因(采用 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:

void test(int arr[], size_t elems) {
   /* ... */
}

int main(int argc, const char * argv[]) {
   int point[3] = {50, 30, 12};
   /* ... */
   test(point, sizeof(point)/sizeof(point[0]));
   /* ... */
}

Also note that, for a similar reason (taking the sizeof a pointer), the sizeof(point)/sizeof(point[0]) trick doesn't work for a dynamically allocated array, only an array allocated on the stack.

夏九 2024-09-10 08:57:14

因为数组衰减为指针作为函数参数传递,因此对于 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.

爱人如己 2024-09-10 08:57:14

另外,了解 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 in test() depending on what was passed in. The sizeof calculation was done when the function was compiled.

江南月 2024-09-10 08:57:14

因为,当它被传递时,实际上只传递了指向数组的指针。

您的问题也在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.

好倦 2024-09-10 08:57:14

因为在 C、C++ 和 Objective-C 中,函数实际上不能有数组参数。它们只能具有看起来像数组参数的参数,但它们不是。在您的示例中,

void test(int arr[])

编译器看到“有一个看起来像 int 数组的参数”,并用“指向 int 的指针”替换该参数。因此,您编写的函数绝对是百分百相同的,

void test (int* arr)

因此,在函数内 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,

void test(int arr[])

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

void test (int* arr)

Therefore, inside the function sizeof (arr) will give you the size of a "pointer to int".

屋檐 2024-09-10 08:57:14

因为 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

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