In C, you can have an array defined locally on in a struct where the length is known.
eg:
{
int foo[8];
assert(sizeof(foo) == sizeof(int[8]));
...
When used in a struct, the array size is also known.
struct MyStruct { int foo[8]; };
void func(struct MyStruct *mystruct)
{
assert(sizeof(mystruct->foo) == sizeof(int[8]));
However when passed as an argument to a function, this is equivalent to an int *
.
void func(int foo[8])
{
assert(sizeof(foo) == sizeof(int[8])); /* will fail */
This is of course correct and to be expected, my question is - whats the terminology to use when referring to the difference?
eg, what would be the correct completion of this comment?
/* This macro will only work correctly when ____ */
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))