Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is an array of size 0 stored in memory?

If I have an array of only one element, we can say it is the same as a pointer. But how's an array of size zero represented in the memory?

What is happening when I declare a variable int * table[0]?

like image 268
Rafael Deleuze Avatar asked Mar 06 '23 13:03

Rafael Deleuze


1 Answers

If I have an array of only one element, we can say it is the same as a pointer

No we cannot. Arrays and pointers are different types and they are represented differently internally. This is true regardless of the array size. Now it is also true that in certain situations (most of the situations actually) an array decays to a pointer to it's first element.

Arrays of size 0 are illegal as per standard, however some major compilers like gcc allow them as an extension.

Read this question to see the difference between internal representations of arrays and pointers: Difference between dereferencing pointer and accessing array elements

like image 72
bolov Avatar answered Mar 10 '23 11:03

bolov