Thread: Understanding offsetof(struct ,member);

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User thriller500's Avatar
    Join Date
    Oct 2011
    Posts
    25

    Understanding offsetof(struct ,member);

    Code:
    #include <stddef.h>
    #include <stdio.h>
    #include <stdlib.h>
    int main(void)
    {    
    struct s 
    {        int i;
            char c;  
          double d;  
          char a[];  
      };   /* Output is compiler dependent */ 
    
      printf("offsets: i=%ld; c=%ld; d=%ld a=%ld\n",(long) offsetof(struct s, i), (long) offsetof(struct s, c), (long) offsetof(struct s, d), (long) offsetof(struct s, a));   
     printf("sizeof(struct s)=%ld\n", (long) sizeof(struct s));
       exit(EXIT_SUCCESS);
    }
    Output : offsets: i=0; c=4; d=8 a=16sizeof(struct s)=16
    My Hypothesis :
    Integer is 4 bytes ..
    its first index so i=0;
    Character is 1 byte ..
    but d=8 it should be 5.Can someone explain me how offset works..
    Last edited by thriller500; 12-21-2012 at 03:20 AM. Reason: Aligning the code

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Indicate the member of the struct
    By Hannibal2010 in forum C Programming
    Replies: 15
    Last Post: 12-10-2011, 09:51 AM
  2. Understanding Struct
    By Oonej in forum C Programming
    Replies: 2
    Last Post: 07-12-2011, 02:17 PM
  3. Assign struct member value to struct member value
    By thahemp in forum C Programming
    Replies: 5
    Last Post: 10-13-2010, 09:48 AM
  4. struct member assignment (c-only)
    By emorrp1 in forum C Programming
    Replies: 8
    Last Post: 06-25-2008, 05:30 AM
  5. Replies: 1
    Last Post: 05-05-2004, 06:58 AM