Domanda

Incomplete array types are used in the famous Struct hack and they are allowed since c99 standard. prior to c99 standard these were not allowed. I was looking at the standard and I am unable to conclude:

Are Incomplete array types allowed outside a structure?(All references I found in the standard C99: 6.7.2.1.15 talk about it as the last element in the structure).

So is the following program allowed to compile as per the standard?

int array[];
int main(){return 0;}

Second part of my questions is, If this is allowed is array guaranteed to be able to store atleast one element of they type int.

È stato utile?

Soluzione

is the following program allowed to compile as per the standard?

Yes, as per:

(C99, 6.9.2p5) "EXAMPLE 2 If at the end of the translation unit containing int i[]; the array i still has incomplete type, the implicit initializer causes it to have one element, which is set to zero on program startup."

So

int array[];
int main(){return 0;}

is valid and equivalent to:

int array[1];
int main(){return 0;}

Note that it is OK only if array has (like above) external linkage as:

(C99, 6.9.2p3) "If the declaration of an identifier for an object is a tentative definition and has internal linkage, the declared type shall not be an incomplete type."

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top