[Solved] Why isn’t the size of my array 4 bytes in C


Ok, I’ll take this one question at a time…

Why isn’t the result of sizeof(foo) 4?

It’s because you’ve only set the size of the foo array to 3 in the first statement char foo[3]. There would be no reason to expect 4 as a result when you’ve explicitly defined the bound as 3 chars which is 3 bytes.

Why isn’t the result 1?

You’re correct in saying that in some cases, calling foo is the same as &foo[0]. The most relevant of these cases to your question is when being passed as a pointer into a function as a parameter. In the case of the sizeof function, when you pass in your foo array pointer, the function iterators throughout the memory block associated with that argued pointer and returns the total size, therefore not being 1.

2

solved Why isn’t the size of my array 4 bytes in C