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; }
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;
"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson
> 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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.