cc compiler warning question.
Gary S. Moss VLD/VMB <moss>
moss at brl.mil
Tue Nov 20 02:08:09 AEST 1990
In article <032D013E619F0000A3 at NRCNET.NRC.CA>, SERRER at NRCM3.NRC.CA (Martin Serrer) writes:
|> #include <stdio.h>
|>
|> typedef struct { int a; int b; } TINY;
|> typedef struct { TINY two; int c; } SMALL;
|> typedef struct { SMALL *one[]; int f; } BIG;
|> BIG *zero;
|>
|> main()
|> { zero = (BIG *) malloc( (sizeof(BIG) + sizeof(SMALL) * 2) );
|> zero->f = 1;
|> zero->one[0].c = 2;
|> printf("--- %i\n",zero->f);
|> printf("--- %i\n",zero->one[0].c);
|> }
|>
|> % cc t.c
|> ccom: Warning: t.c, line 18: illegal zero sized structure member: one
|> } BIG;
|> --^
You are declaring "one" as an array of unspecified, therefore zero size,
declare it as "SMALL **one;" instead. In this context, there is a big
difference between the two declarations; the compiler knows how big a
"SMALL **" is.
|> ccom: Warning: t.c, line 28: struct/union or struct/union pointer required
|> zero->one[0].c = 2;
|> ---------------^
You must use "->" rather than ".":
In the declaration "SMALL *one[];", one is an array of pointers to SMALL types.
Therefore, one[0] is a pointer and requires a "->" after it, not a ".".
|> ccom: Warning: t.c, line 29: struct/union or struct/union pointer required
|> printf("--- %i\n",zero->one[0].c);
|> -------------------------------^
Same thing as above.
You will also need to allocate storage for "one" separately from "zero":
if( (zero = (BIG *) malloc( sizeof(BIG) )) == NULL
|| (zero->one = (SMALL *) malloc( sizeof(SMALL) * 2 )) == NULL )
{ /* ALWAYS check success of malloc(3). */
(void) fprintf( stderr, "No memory available!\n" );
return 1;
}
Of course, you *could* declare storage for zero like this:
BIG zero;
Rather than the first call to malloc, and leave your "."s as they are.
-Gary
More information about the Comp.sys.sgi
mailing list