arraysclanguage-lawyeransi-cincomplete-type

C: Allowed to assign any array to pointer to array of incomplete type


The following seems of questionable legality in C89. I can't figure out why it's allowed. I'm reading the standard and my copy of K&R2 and I still don't get it.

char Arr[16];
char (*Durr)[] = &Arr; /*Why is this allowed?*/

That's it really. I need a quote from the C89 standard that tells me why this is permitted. This is not a C++ question, it's definitely illegal there. Thanks!

EDIT: This explains where it comes into question in the standard: http://port70.net/~nsz/c/c89/c89-draft.html#3.3.16.1


Solution

  • The initialization in char (*Durr)[] = &Arr; requires Durr pointing to an array of type compatible with the type of Arr.

    According to "6.7.6.2 Array declarators" (n1570)

    6 For two array types to be compatible, both shall have compatible element types, and if both size specifiers are present, and are integer constant expressions, then both size specifiers shall have the same constant value.

    Because the array pointed to by Durr has an incompleted type, which implies that those two types should be compatible, then compiler should not give error/warning for this initialization.