3

Is it a WARRANTY, that offset of first element of structure is 0? To be more accurate, lets consider

struct foo {
int a;
double b;
};
struct foo *ptr=malloc(sizeof(struct foo));
int *int_ptr = &ptr->a;
free(int_ptr)

Is it garantied, that it is valid always, under any os or any other factors?

KAction
  • 1,977
  • 15
  • 31

1 Answers1

6

Yes it is guaranteed. Will get you a standard quote, let me lookup.

C99 Standard: §6.7.2.1

Para 12

Within a structure object, the non-bit-field members and the units in which bit-fields reside have addresses that increase in the order in which they are declared. A pointer to a structure object, suitably converted, points to its initial member (or if that member is a bit-field, then to the unit in which it resides), and vice versa. There may be unnamed padding within a structure object, but not at its beginning.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • 1
    §6.7.2.15 seems about right: "*A pointer to a structure object, suitably converted, points to its initial member (or if that member is a bit-field, then to the unit in which it resides), and vice versa. **There may be unnamed padding within a structure object, but not at its beginning**.*" – DCoder Jun 09 '12 at 05:27
  • @DCoder: Ah, You beat me to that :) – Alok Save Jun 09 '12 at 05:29