Home > Backend Development > C++ > Why Does `sizeof()` Return Incorrect Array Sizes When Passed to C Functions?

Why Does `sizeof()` Return Incorrect Array Sizes When Passed to C Functions?

Patricia Arquette
Release: 2025-01-01 10:41:11
Original
212 people have browsed it

Why Does `sizeof()` Return Incorrect Array Sizes When Passed to C Functions?

Understanding the Discrepancy in sizeof() Values for Arrays Passed to Functions in C

In C, arrays can exhibit an unexpected behavior when passed to functions. The question arises: why does the sizeof() operator return an incorrect value when an array is passed to a function, while it provides the correct value before the function call?

To address this, we must understand that arrays in C undergo pointer decay when passed to functions. When an array is passed, it degenerates into a pointer pointing to the array's first element. Consequently, using sizeof() on the function parameter evaluates the size of the pointer instead of the entire array.

A typical example is given in the question:

#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;
}
Copy after login

In the main() function, the correct array size is determined by dividing the size of the allocated memory (obtained using sizeof(point)) by the size of each element (sizeof(point[0])). However, in the test() function, the calculated array size is incorrect because sizeof(arr) returns the size of the pointer to the array, not the array itself.

To resolve this issue and allow the function to determine the array's size, it should be passed 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]));
   /* ... */
}
Copy after login

It's important to note that the sizeof(point)/sizeof(point[0]) trick only works for arrays allocated on the stack. For dynamically allocated arrays, it will return an incorrect result, as the size of the array is not stored with the pointer.

The above is the detailed content of Why Does `sizeof()` Return Incorrect Array Sizes When Passed to C Functions?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template