0
#include <stdio.h>

int main()
{
  struct name
  {
    int x;
    struct name *ptr;
  };

  struct name a;
  printf("size is %d",sizeof(a));
  return 0;
}

The result of this code is 16 bytes.How compiler has calculated the size of *ptr?

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • 5
    probably 4 (int) + 4 (padding) + 8 (pointer) – Jean-François Fabre Aug 24 '17 at 13:22
  • 1
    note that it's 16 bytes on _your_ machine given _your_ compiler & compilation options. – Jean-François Fabre Aug 24 '17 at 13:23
  • Please format your code correctly so it is readable (it's done now). – Jabberwocky Aug 24 '17 at 13:52
  • @KlasLindbäck "All pointers (except function pointers, which may have a different size) have the same size." --> Certainly common, yet I do not see this as specified. Any citation to support? See "Pointers to other types need not have the same representation or alignment requirements." C11 §6.2.5 28 – chux - Reinstate Monica Aug 24 '17 at 14:48
  • @KlasLindbäck: That's not guaranteed - pointers to different object types *may* have different sizes and representations. What *is* guaranteed is that all `struct` pointer types have the same size and representation as each other. Yes, on most modern systems, all pointer types have the same size and representation, but you can't rely on it being universal. – John Bode Aug 24 '17 at 17:19
  • @chux JohnBode You are both correct. My mistake. – Klas Lindbäck Aug 25 '17 at 06:45

0 Answers0