Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!wuarchive!usc!ucsd!ucbvax!SLIC.CELLBIO.DUKE.EDU!jit From: jit@SLIC.CELLBIO.DUKE.EDU (Jit Keong Tan) Newsgroups: comp.sys.sgi Subject: (none) Message-ID: <9011200509.AA03725@slic.cellbio.duke.edu> Date: 20 Nov 90 05:09:25 GMT Sender: daemon@ucbvax.BERKELEY.EDU Organization: The Internet Lines: 73 Subject: Re: cc compiler warning question. #include typedef struct { int a; int b; } TINY; typedef struct { TINY two; int c; } SMALL; typedef struct { SMALL *one; int f; } BIG; /* ^^^ corrected, I like to think in terms of linear address space instead of two-d array. Besides *[] is an array of pointers. Since the size of the array is not known yet, the structure size is unknown ---> it is illegal, C compiler needs to know this. Whereas SMALL **one is the same size as SMALL *one; they only differ in interpretation. You can declare it as SMALL **one but I am quite sure this is not you want. */ typedef struct { SMALL **one; int f; } TBIG; BIG *zero; SMALL *sptr; main() { printf("sizeof(TBIG) %u bytes == sizeof(BIG) %u bytes\n", sizeof(TBIG),sizeof(BIG)); /* >>>>{ zero = (BIG *) malloc( (sizeof(BIG) + sizeof(SMALL) * 2) ); >>>> conceptually wrong, this is not you want */ if ( (zero = (BIG *) malloc(sizeof(BIG))) == NULL) { printf("MALLOC error\n"); /* Not enough memory */ exit(1); } if (( sptr = (SMALL *) malloc(2*sizeof(SMALL))) == NULL){ printf("Malloc SMALL: out of memory\n"); exit(1); } zero->one = sptr; zero->f = 1; zero->one->c = 2; /* No subscript is equivalent to [0] */ /* or zero->one[0].c = 2 if you like */ (sptr+1)->c = 4; /* Accessing the second element, C compiler will take care of the size increment. */ printf("--- %d\n",zero->f); printf("--- %d %d\n",sptr->c,(sptr+1)->c); printf(" %d == %d == %d; %d == %d\n", sptr->c, zero->one->c, (*zero->one).c, /* Not zero->one.c because zero->one is a pointer and you want to get the content of the pointer. */ (sptr+1)->c, zero->one[1].c); /* should be equal */ exit(0); } -------------------------------------------------------- Jit Keong Tan | internet: jit@slic.cellbio.duke.edu (919) 684-8098 | bitnet : tan00001@dukemc.bitnet -------------------------------------------------------- U.S. Mail: Duke University Medical Center Department Of Cell Biology Box 3709 Durham, NC 27710