-2

Possible Duplicate:
What is the actual size of a struct in C

I wrote the following program in C and I'm a little confused about the size of the struct that the executable prints. Here is the code:

#include <stdio.h>
#include <stdlib.h>

struct domi1{
   char x;
   int c;
   char y;
};

struct domi2{
   char x;
   char y;
   int c;
};

main(){

 printf("The size of domi1 is: %d\n", sizeof(struct domi1));
 printf("The size of domi2 is: %d\n\n", sizeof(struct domi2));

 system("pause");
}

The program prints 12 as the size of domi1 and 8 as the size of domi2. Why is that? I appreciate your help!

Community
  • 1
  • 1
mgus
  • 808
  • 4
  • 17
  • 39

1 Answers1

-1

because of Packing and byte alignment
The general answer is that compilers are free to add padding between members for alignment purpose. or we can say that, You might have a compiler that aligns everything to 8 bytes. first struct assigned memory as- 4 4 4
second struct- single 4 for both char and 4 for int

Ravindra Bagale
  • 17,226
  • 9
  • 43
  • 70