size of structure = 12Code:#include <stdio.h>
struct point
{
int x;
char y;
float z;
}var;
int main()
{
printf("size of structure = %d", sizeof(var));
return 0;
}
Printable View
size of structure = 12Code:#include <stdio.h>
struct point
{
int x;
char y;
float z;
}var;
int main()
{
printf("size of structure = %d", sizeof(var));
return 0;
}
Lookup the pragmas for your compiler.
For some simple compilers the order will make a difference. Put the biggest items first. As in.
Tim S.Code:struct point
{
float z;
int x;
char y;
}var;
> size of structure = 12
With those three members, it's always going to be 12 no matter how you arrange them.
If you succeed in making it 9 through some pragma, then the compiler will generate very sub-optimal code, because it will have to assume that both the int and float are unaligned. In doing so, it will read each one a byte at a time in order to construct a properly aligned temporary.
Be careful what you're saving.