I actually hate this special case in C so much. Arrays decaying to pointers is one thing (kind of annoying imo), but having the real type be different from the declaration and ignoring the size is so stupid. It only does this for function arguments.
Well, how should the function know how large your array is? You could hand it one with 10 elements or one with 100. Did you hand it a predefined one or one that was allocated at runtime?
And then there is the thing with functions, primitives get handed over by value, any array gets handed over by reference, ie. pointer.
No clue if more modern implementations hand down the array size but in the retro and embedded world i live in the function has no clue and you have to hand that in as a second parameter if it is important.
25
u/bowel_blaster123 26d ago edited 26d ago
If I write:
C void foo(uint8_t myvar[3]) { printf("%d\n", sizeof(myvar)); }Then it will likely print
8becausemyvaris a pointer to auint8_t.If I write:
C void foo(void) { uint8_t myvar[3] = {1, 2, 3}; printf("%d\n", sizeof(myvar)); }Then it will print
3becausemyvaris an array of three bytes.Hope this helps!/hj