Not if sizeof (struct header) is not a multiple of the alignment
requirement for struct xyz.
Consider a system where int is aligned on a multiple of 4 and
struct header {char c;};
struct xyz {int i;};
sizeof (struct header) could be 1 (there is no need for any padding
after c). and sizeof (struct xyz) could be 4. struct xyz must be
aligned on a multiple of 4 to insure i is aligned.
No sizeof(struct header) would be 4 when the alignment requirement of
a struct is a multiple of 4. Don't work with implementation defined
extensions (e.g. #pragma align) in that case. ANCI C requires that
each member of an array is on an aligned address. struct header[100]
must be 400 bytes in size to have 100 structs of that type aligned
right.
sizeof(char) is always 1. sizeof(struct) is always (sizeof member
rounded up to alignment for any type) + (sizeof next member rounded up
to alignment for any type) + ......
So the single char in the struct has a number of padding bytes to get
the struct the right size to come to the next aligned address. Check
it with any ANSI C compiler you can get in your hands - or simply read
the standard carefully.
The sum of the two sizeof values will allocate 5 bytes aligned on a
multiple of 4 which is not sufficient to hold a struct header followed
by a struct xyz.
Wrong. a char is not a stuct, even as a struct may contain (only) a
char, so the size of struct and char differs.
Use sizeof to get the size of a struct. add the number of of structs
to a pointer, cast the pointer to another struct type - and you're
guaranteed that this new pointer is well aligned to point to an
aligned stuct of that type. The size of an struct doesn't matter. It
is portable, it is ANSI C.
The size of an struct can differ from implementation to implementation
because each implementation has its own alignment requirements and the
size of a struct is based on the align requirements of each of its
members (including the last one). A struct is calculated:
size in bytes of 1. menber + padding bytes to get an address able to
hold an aligned object of any type
+ size in bytes ...
+ ...
So the size of a struct ends up so that a pointer to an aligned object
increased with the size of a struct results in a pointer to an aligned
object of any type.
Define a char! define thereafter another char - the compiler will
create both on different addresses - and each address will differ in
sizeof(char) + required bytes to get the next address aligned to hold
each other other object. It is always the job of the compiler to
define objects on aligned addresses.
When you plays around with implementation defined things (such as
#pragma align) then you leaves ANSI C and you gets unportable. In ANSI
C you can always count on the compiler that pointer arithmetic works
right, so whenever you have to build a pointer you gets a well aligned
pointer when you works on a well aligned pointer.
Increasing a pointer with a number of elements of that type results
always in an well aligned pointer of that type. All structs have equal
alignment requirements to get the next struct.