Forward referencing with typedef
David Brown
david@westcontrol.com
Fri Dec 9 07:44:00 GMT 2011
On 09/12/2011 08:26, Divyesh wrote:
>
> Hi,
>
> Please refer to the following code snippet.
>
> struct strArr;
>
> typedef struct strArr myStAr[17];
>
> typedef struct
> {
> unsigned short DISPLAY_:1;
> } structDisp;
>
> typedef structDisp disp;
>
> struct strArr
> {
> disp dispBit;
> } ;
>
> int f()
> {
> return 0;
> }
>
>
> In the code we have a typedef to an array of struct.
>
> Now since the definition of that struct is given after the use, gcc cribs.
>
> The error reported by gcc is : array type has incomplete element type.
>
> Interesting thing is gcc 3.4.4 compiles the code where as gcc 4.3.4 and 4.6
> doesn't.
>
> I think this is a gcc bug.
>
> Request assistance for the same.
>
> Thanks and Regards,
> Divyesh
It is not a bug. It is interesting that gcc 3.4.4 accepted the code, as
it is (AFAIK) invalid C.
It is easy to see what is going on when you think about how the compiler
will interpret this - starting from the top and moving down. The usual
use of forward struct declarations is so that you can make pointers to
them in a circular type definition - that works fine, because the
compiler can conceive of a "pointer to struct" without knowing the
details of the struct. But the compiler cannot work with an instance of
the forward-declared struct, or an array of them, or use it as an
element in another struct (only pointers are allowed) - all these things
require a knowledge of the size of the struct.
More information about the Gcc-help
mailing list