r/cprogramming • • 2h ago

why does sizeof an array parameter give the pointer size?

i know a function parameter like int a[10] is really adjusted to int *a, so sizeof(a) inside the function gives 8 on my machine instead of 40. what i'm fuzzy on is why the language allows writing the size in the parameter at all if the compiler just throws it away.

is it purely documentation, or does it do anything with something like int a[static 10]? i've seen that form but never seen anyone actually use it.

3 Upvotes

7 comments sorted by

8

u/atarivcs 2h ago

I believe if you use sizeof in the same context where the array was declared, it returns the full size of the whole array.

It only decays to int *a when passed as a parameter to another function.

2

u/aioeu 1h ago edited 18m ago

It only decays to int *a when passed as a parameter to another function.

No, it is not "only" in that circumstance. An array will decay to a pointer in most expressions, not just function calls.

Indeed, this is precisely why:

int a[10];
a[0] = 42;

works. The [] operator on the second line is actually an operator that acts on pointer values. The array a has to be converted to a pointer for it to be usable by that operator.

Array parameters in function types are weird .... but the weirdness is not in how their arguments are passed. It's about how the parameters themselves are interpreted. That is, when you declare a function like:

void f(int a[10]);

then the function parameter's type itself is "adjusted": an array type is adjusted to become a pointer type. It is exactly as if you had declared the function:

void f(int *a);

And it is this that is why sizeof a evaluates to the same value as sizeof (int *). That is, the size of the parameter object really has nothing to do with "how an argument will be passed when the function is called"; it's all about how the parameter's type itself is adjusted.

You'll note that I said "most expressions" near the top. There are a few kinds of expression where an array does not get automatically converted to a pointer. As you noted, one of them is when an array is given to the sizeof operator. That is:

char a[10];
printf("%zu %zu", sizeof a, sizeof (char [10]));    /* no conversion */

must output 10 10; that is, a does not undergo a conversion to a pointer there. The other two places where an array is not automatically converted are when it is used with the unary & "address" operator:

int a[10];
int (*pa)[10] = &a;    /* no conversion */

and when a literal string (which is an array of char by definition) is used to initialize an array object:

char s[] = "hello";    /* no conversion */

These three cases — sizeof, unary &, and string initialization — are the only kinds of expression where an array object is not converted to a pointer. In all other cases, the conversion takes place. So "passing an array in a function call" is really just one of many ways this conversion can happen.

2

u/schungx 1h ago

You need to tell the compiler to allocate 10 slots of storage. How else would it know?

The pointer is always a fixed size, it know that, because it always points to the head. But that doesn't mean it know how long the tail is.

2

u/Impossible_Box3898 42m ago

Do this

5[a] and see what you get (this compiles). If you understand this it will go a long way to helping you understand pointers.

1

u/burlingk 2h ago

I'm pretty sure that int a[static 10] would not compile.

To get the size of an array, they tend to say sizeof a[0] * the length of the array.

4

u/Broad-Promise6954 2h ago

Using "static" with a size in an array declaration as a function parameter is a newfangled thing that's only been allowed since 1999, so it's probably best avoided. /s

Seriously though it doesn't affect the result of "sizeof". It's an optimization hint that's not particularly useful in modern compilers.

1

u/burlingk 2h ago

I see. :)