[LCP]struct & union

Bill Rausch BRausch at owt.com
Tue Feb 5 17:19:53 UTC 2002


At 10:12 AM +0530 2/4/02, shahnavaz wrote:
>hi,
>	Can someone tell me why the sizeof returns 4 in the main 
>function.Even if i
>use int in place of float it returns 4.
>
>TIA
>shan
>
>
>#include<stdio.h>
>struct s{
>	float data;
>};
>struct c{
>	float c;
>};
>typedef union {
>	struct s ss;
>	struct c cc;
>}Union;
>
>int main(void)
>{
>	Union u;
>	printf("Size of Union is  %d\n",sizeof(u));
>	return 0;
>}

Only one of the elements in a union can exist at any given time. The 
space reserved has to hold the single largest item. In your example, 
both items are the same size.

The sizeof int and the sizeof float depend on the compiler, not the 
CPU. The compiler you are using has 32 bit ints and 32 bit floats.


-- 
Bill Rausch, Software Engineer

   "It's the World Wide Web." he said. "What's it do?" I asked. "I don't know,"
   he answered, "they haven't finished it yet."



More information about the linuxCprogramming mailing list