Search code examples
cportabilitypointer-arithmetic

Are adjacent structure members of equal type adjacent in memory?


In the language described by ISO 9899:2011 (that is, C11), with T being a complete data type and the declaration

struct { T a, b; } s;

can I assume that &s.a + 1 == &s.b? Did the situation change in between revisions of the C language?


Solution

  • No. The standard places no requirement on structure padding or lack of it, other than that there cannot be initial padding before the first member.

    Each structure member must be correctly aligned for its type, of course.