3

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

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
Subsentient
  • 554
  • 3
  • 12

1 Answers1

1

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.

Lee Duhem
  • 14,695
  • 3
  • 29
  • 47
  • I believe this would be the same text as 3.5.4.2 in C89. I only have some ancient draft of C89 so I'm not sure. C89 is quite obsolete... – Lundin May 06 '14 at 08:04
  • @Lee: Thank you. That is exactly the kind of quote I was hoping for. This was a very competent answer. Perhaps you can get me the standard text for this too? This question has a bounty. http://stackoverflow.com/questions/20951091/c-is-it-legal-to-subscript-an-array-of-incomplete-type – Subsentient May 06 '14 at 08:16
  • 2
    @Subsentient C99 and C11 fixed many big flaws and insanities in the C language. For that reason alone, C89 is obsolete. Whether or not you chose to use the completely new features of C99/C11 is another matter. – Lundin May 06 '14 at 09:04
  • `Because the array pointed to by Durr has an incompleted type`! Isn't the size of `Arr` `16`? – ajay May 06 '14 at 12:02
  • @ajay You missed "if both size specifiers are present". – Lee Duhem May 06 '14 at 12:15