r/cprogramming • u/CelebrityChess • 6d 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.
21
Upvotes
21
u/atarivcs 6d 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.
6
u/Impossible_Box3898 6d 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/Telephone-Bright 6d ago
Many features were put in C for the benefit of C compiler-writers. They were the main customers for the first few years.
E.g. arrays beginning with the index zero instead of one,
autokeyword, no nested functions,registerkeyword, etc.Arrays decaying into pointers is one of such features. It is simpler to treat arrays as pointers.
When you have simple stuff like
int a[10];, the compiler does throw away the number10. It's documentation here.When it comes to
int a[static 10]in C99+, it's basically a promise to the compiler like "Yo I guarantee that this array's neverNULLand is a pointer to the first element of an array with at least 10 elements".1
u/pskocik 6d ago
I believe it started as a B-language compatibility thing and stuck around for documentation.
Only the innermost [] is adjusted to a pointer. The subsequent ones are kept. So you can pass a pointer to array or pointers to array if you want to keep the element counts. It works with dynamic sizes too thanks to the possibility of VLA-pointers (which need not be VLA-allocated -- you can use them even with malloced memory, for example).
1
u/DawnOnTheEdge 5d ago
For historical reasons, C has array arguments decay to pointers. Similarly, the language doesn’t allow you to directly return an array object, only a pointer (which cannot be to a variable local to the called function, or it will be left dangling when that is destroyed on return) or a structure containing an array.
One way around this is to pass the function a pointer to a complete array object of known constant size, such as `void foo(int(*bar)[3])`. In that case, `sizeof(*bar)` will work. In most cases, thugh, you will pass the number of elements in each array to the function as a separate parameter.
1
u/Paul_Pedant 5d ago
One fairly important purpose for
int a[10]is to suggest to the compiler that it actually needs to reserve 40 bytes within the generated program to accommodate the data. That applies for global or static arrays, or for reserving space on the stack, depending on where it is declared.The compile does however expect the coder to remember the size of the array if it needs to be passed to other functions. That can be done through a
#defineor as a separate variable. It sucks to pass it around as an actual integer, because then you end up having to search the whole code when it changes, to decide whether any particular 10 is your array size, or something else completely different.1
u/W31337 5d ago
Because the pointer points to the first item?
1
u/sciencekm 5d ago
If you want to preserve the array length, put it in a structure.
1
u/__rituraj 5d ago edited 4d ago
arrays in C are not first class candidates! in fact, they are just syntactic sugar.
yes, it Array is not an actual type in C.
`int a[10]`
is basically
`int *a =stackalloc(10 * sizeof (int))`
where stack alloc is jus there to represent that its allocated on the stack.
and similarly,
`a[5]` is internally just `*(a+5)`
Its the most raw form of array which hardware can understand. That's what it is in C
EDIT: removed spoiler tags
1
u/ern0plus4 5d ago
a[10]is an array of 10,*ais a pointer to an array, what's the question? ;) Same:char xis 1-byte,*xis a pointer, so it's 8 (or 4 on 32-bit systems) byte. Notice,xis not an array, just a single char, but a pointer is always 8 (4) byte regardless of the type/size/whatever it points to (whatever: a pointer can point to an unknown or any type, akavoid*).A pointer is only an address of something, and an address can't tell whether the target is an array or a single item, or how many elements it has. A pointer can point to a JPG file loaded into memory - still the pointer is 8 (4) byte.
The compiler knows the item's type, so the size, and
a[n]will point to the nth element (you don't have to multiplynby the size of item), but it does not know the number of elements, anywaym it can be changed runtime.1
u/SmokeMuch7356 4d ago
Yeah, mostly.
Brief history lesson:
C was derived from Ken Thompson's B programming language. In B, an array declaration like
would set aside an extra word to store the address of the first element:
The array subscript operation
a[i]was defined as*(a + i)- offsetiwords from the address stored inaand dereference the result.An interesting side effect of this was that writing
effectively declared
pas a pointer. Space was set aside for the objectpeven if no array elements existed.I suspect this is why the
notation survives in modern C (even though C doesn't allocate that separate pointer object); it was a valid way to specify a pointer in B, what the function actually receives is a pointer, and since C carries so much heritage from B it makes sense to keep that notation.
And from there it's not a great leap to evaluate the size expression but not do anything with it.
The declaration
is intented to be a hint to the compiler that
apoints to the first element of an array that's at least 10 elements wide. I don't know how useful that is, or how widely it's supported.1
u/binarycow 4d ago
The easiest (and most portable) way to do code blocks is to start the line with four spaces.
1
u/flyingron 3d ago
Because you can't pass (or return) arrays from functions. It's a tremendous stupid inconsistency in the language. Any time there is an array (even of definite size) as a parameter or return type, the language treats it as a pointer, and hence sizeof returns the pointer size.
1
u/EatingSolidBricks 3d ago
Look up array to pointer decay
1
u/schungx 6d 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.
0
u/burlingk 6d 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.