0

I am trying to understand the structure concept in C with array of pointers.Here is my code snippet

typedef struct Student
{
   char name[20];
   int marks;
   struct Student *a_ptr[10];
}stu;

main()
{
  stu member;
  printf("member : %d\n",sizeof(member));
}

My question is, In main function when I am trying to find sizeof(member) I am getting the value as 104 bytes. I can understand that char name[20] and int marks holds 24 bytes. I can't understand how compiler calculates the rest of the 80 bytes for struct student *aptr[10].

Karthi13
  • 119
  • 2
  • 11
  • 2
    This is not a duplicate of the linked question in any way. Anyway, your platform probably has 8-byte pointers, and your structure contains an array of 10 pointers. `8*10 == 80`. What's confusing about that? – EOF Sep 01 '15 at 09:06
  • Are you on a 64-bit system? – Some programmer dude Sep 01 '15 at 09:07
  • yes I have 64-bit system @Joachim – Karthi13 Sep 01 '15 at 09:23
  • 1
    @SUGMAR Then you have your answer: Pointers are usually the same size as the native words in this case 64 bits), and 64 bits is 8 bytes, and 8*10 (the `a_ptr` array size) is 80. – Some programmer dude Sep 01 '15 at 09:31
  • thank you so much @Joachim I can get hold of this now. The confusion arised because the book which i am using says that size of a pointer is 4 bytes. – Karthi13 Sep 01 '15 at 09:51
  • @SUGMAR The pointer size depends on the architecture. If your book just says it is always 4 byte, maybe get a better book. – deviantfan Sep 01 '15 at 10:14

0 Answers0