[LCP]struct & union

Chirag Kantharia chyrag at yahoo.com
Mon Feb 4 15:54:10 UTC 2002


On Mon, Feb 04, 2002 at 10:12:31AM +0530, shahnavaz wrote:
| 	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.

Because sizeof(int) is 4 (at least on linux on i386). And sizeof a union
is the max size of the members within.

| #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;
| }

chyrag.
-- 
Chirag Kantharia, symonds.net/~chyrag/
Linux scrooge 2.4.17 #1 Wed Jan 16 17:07:25 IST 2002 i686 unknown




More information about the linuxCprogramming mailing list