We have following structs:
struct parent {
char *name;
};
struct child {
struct parent parent;
int other_child_specific_data;
};
And also we have function which works with struct parent
:
void print_parent_name(struct parent *p)
{
printf("%s\n", p->name);
}
Question: can I be sure, that calling that function like this:
struct child c;
c.parent.name = "Child #1";
print_parent_name(&c);
will be always the same as
print_parent_name(&c + offsetof(struct child, parent));
?
In other words, the question is: will be offsetof(struct child, parent)
always equal to zero if struct parent parent
is placed as first definition at struct child
?
May be somebody can give a ref to C standard? And are there such hardware or software configurations that make offsetof(struct child, parent)
in such structs not equal to zero?