Thread: Accessing part of an unknown structure

  1. #1
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591

    Accessing part of an unknown structure

    Confirm: safe and portable?
    Code:
    int main( void ) {
     struct {
      int magic;
      void *data1;
      void *data2;
      ...
      void *datan;
     } args = { 0xdeadbeef };
    
     myfunc( &args );
    }
    
    myfunc( void *arg ) {
    int magic = ((struct { int magic; } *)arg)->magic;
    ...
    }
    My guess is C struct elements are stored linearly, so this will be safe... but I'm not 100% certain.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Doesn't offsetof() work?

  3. #3
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    thanks for pointing out that macro, but how can I use it without knowing the struct type?
    The myfunc function does not know the exact kind of struct that is being passed (it can be one of many different structs). All it knows is that the first member is an integer magic code.

    So I need a portable and safe way of getting the first member of a struct without knowing what lies in the rest of the struct.

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Standard grantees that there'll be no padding in front of first struct member.
    It should be portable and safe.
    The Standard makes some guarantees about the layout of structures and unions:
    Members of a structure are allocated within the structure in the order of their appearance in the declaration and have ascending addresses.
    There must not be any padding in front of the first member.
    The address of a structure is the same as the address of its first member, provided that the appropriate cast is used. Given the previous declaration of struct wp_char, if item is of type struct wp_char, then (char *)item == &item.wp_cval.
    Bit fields (see Section 6.4) don't actually have addresses, but are conceptually packed into units which obey the rules above.
    Resource

  5. #5
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    excellent thanks Bayint! that's exactly the reassurance I was looking for.
    I will cast the (void *) argument to the type of the first member.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Passing Structure Reference To Function
    By soj0mq3 in forum C Programming
    Replies: 9
    Last Post: 04-24-2010, 10:27 AM
  2. passing structure arrays to functions?
    By bem82 in forum C Programming
    Replies: 3
    Last Post: 10-30-2006, 06:17 AM
  3. Help filling a structure with data from a file.
    By System_159 in forum C++ Programming
    Replies: 41
    Last Post: 09-27-2006, 06:39 PM
  4. Structure - Typedef accessing problem
    By dayknight in forum C Programming
    Replies: 4
    Last Post: 08-11-2006, 11:52 PM
  5. accessing structure pointer problem
    By godhand in forum C Programming
    Replies: 2
    Last Post: 04-09-2004, 10:52 PM