--- In [email protected], "una_020" <[EMAIL PROTECTED]> wrote:
>
> --- In [email protected], "John Matthews" <jm5678@> wrote:
> >
> > --- In [email protected], "una_020" <una_020@> wrote:
> > >
> > > 1.how to find size of structure without using sizeof operator?
> > 
> > Make an array of structures and find the difference in addresses of 2
> > consecutive array elements. But as David said, you would use sizeof 
> in
> > a real program.
> >
> this wudnt work coz according to array arithmetic the difference 
> between the addresses of adjacent elemenst wud give 1.

You use casts eg.

#include <stdio.h>

typedef struct
{
    char a;
    int  i;
} SomeStruct;

int main(void)
{
    SomeStruct a[2];

    printf("size = %d\n", (unsigned)&a[1] - (unsigned)&a[0]);

    return 0;
}

When run (gcc, Linux):

size = 8

Reply via email to