Tech ID said:
struct pad1 {
char c1;
};
struct pad2 {
char c1;
int x;
};
struct pad3 {
char c1;
char c2;
int x;
};
struct pad4 {
char c1;
char c2;
int x;
struct pad1 {
char c1;
};
This should be something like:
struct pad4 {
char c1;
char c2;
int x;
struct pad1 p1;
};
What will be the sizeof for the structs above? (for 32-bit and 64-bit
machines).
It's not clear to me how the question in your subject header and
the question in the body of your article are related.
The best (in some sense) answer to your second question (once
you correct the error in the declaration of struct pad4) is:
sizeof (struct pad1), sizeof (struct pad2), sizeof (struct pad3),
and sizeof (struct pad4), respectively. I know that's not the answer
you were looking for, but in most contexts it's all you need to know.
As for the first question, "Are struct members ... also padded?",
what exactly do you mean by that?
A struct has a size and layout determined by the compiler.
The compiler may add padding after any member, including the last
one. This is normally done to satisfy alignment requirements,
but in theory a compiler could add padding for any reason.
Your struct pad4 has a member of type struct pad1, which I've
named p1. p1 has whatever size struct pad1 has, which will include
any padding after c1. There may or may not be additional padding
after p1; this padding would be part of struct pad4.
Does that answer your questions?