(none)
Jit Keong Tan
jit at SLIC.CELLBIO.DUKE.EDU
Tue Nov 20 16:09:25 AEST 1990
Subject: Re: cc compiler warning question.
#include <stdio.h>
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 at slic.cellbio.duke.edu
(919) 684-8098 | bitnet : tan00001 at dukemc.bitnet
--------------------------------------------------------
U.S. Mail:
Duke University Medical Center
Department Of Cell Biology
Box 3709
Durham, NC 27710
More information about the Comp.sys.sgi
mailing list