Add the size of your structure's members and you will have the structure's size!
The size of MY_STRUCT would be sizeof(int) + sizeof(float).
No generally this is not true.
- MY_STRUCT myStruct;
-
unsigned char myStructSize = sizeof(myStruct);
-
unsigned char myStructSizea = sizeof(myStruct.a);
-
unsigned char myStructSizeb = sizeof(myStruct.b);
-
If you do this you need to be very careful because on the majority of platforms
myStructSize != myStructSizea + myStructSizeb
because the compiler puts padding bytes into the structure to maintain correct member alignment for efficient access.
sizeof(myStruct) will give you the size of the structure including any padding bytes, for example should you want to use malloc to allocate memory for it.
The are some platforms, typically 8bit ones where the compiler does not include any padding bytes on which the size of the structure is the sum of the size of its members.