when i prints struct type variable it prints more byte then it has data type bytes.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • my098
    New Member
    • Feb 2017
    • 1

    when i prints struct type variable it prints more byte then it has data type bytes.

    when I declare
    struct student{
    int rollno; //it reserve 4 bytes
    char name[25]; // it reserve 25 bytes
    float marks; // it reserve 4 bytes
    // total is 33byte
    };

    struct student s1; //it reserve 33 bytes
    printf("%d",siz eof(s1));
    // it prints 36.
    // why prints 36 intead 33 because its total bytes is 33..
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Yes, that can happen. When the compiler sees the struct, it tries to align the members on integer boundaries to simplify locating the members in memory.

    I this case the int is on an int boundary and you reserve 4 bytes. The array takes 25 bytes so it extends into the next int area by one byte. The compiler then adds 3 pad bytes so the float starts on the next int boundary.

    Comment

    Working...