regarding char array size

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zxc123
    New Member
    • Dec 2007
    • 1

    regarding char array size

    #include <stdio.h>
    main(){
    int i=0;
    struct emp
    {
    char t[6];
    int x;
    int y;
    };
    struct emp e = {"ram",304,678} ;
    printf("%u\t%u\ t%u\n",&e.t,&e. x,&e.y);
    }

    output:
    2280780 2280788 2280792

    --------------------------------

    Is the (char t[6]) taking 8 bytes?

    what is that extra two bytes b/w char array and (int x)
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Those are pad bytes.

    struct members usually align on an int boudary so the compiler had to add 2 bytes to align x on an int boundary.

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Your format string doesn't match the types of any of the three values you're printing. That leads to undefined behavior -- which is a very bad thing. You should use %p instead of %u. Another way to inspect memory organization within a structure is to print the value of offsetof().

      Comment

      Working...