offsetof

This is a discussion on offsetof within the C Programming forums, part of the General Programming Boards category; <<split from http://cboard.cprogramming.com/showthread.php?t=40936 >> If I call this offsetof() macro in a function: Code: int func([type] struct_name, [type] member) { ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    98

    offsetof

    <<split from http://cboard.cprogramming.com/showthread.php?t=40936>>

    If I call this offsetof() macro in a function:
    Code:
    int func([type] struct_name, [type] member)
    {
       ...
       x = offsetof(struct_name, member);
       ...
    }
    What should be the [type]s in the function prototype? That is, what are the types of the parameters of offsetof()?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    > What should be the [type]s in the function prototype?
    You can't.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    How are we supposed to know?
    Post the real code.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    offsetof is a macro, not a function, and the first argument is a struct or union, the second a member of said struct/union.

    Eg.
    Code:
    struct blah
    {
       int x;
       int y;
    };
    
    int main()
    {
       printf("offsetof(blah, y)= %d\n", offsetof(blah, y));
       return 0;
    }
    Pedantisizm prevention: If we are usign C99, we probably should use %zd as the format specifier, as offsetof forms a size_t value, and that should use the z modifier to tell printf that it is of "size_t" size, rather than regular int.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Just some messing about...
    Code:
    #include <stddef.h>
    #include <stdio.h>
    
    struct T
    {
       int i;
       double d;
       char c;
       char s[10];
       float f;
    };
    
    int Toffset(int member)
    {
       switch ( member )
       {
       case 0: return offsetof(struct T, i);
       case 1: return offsetof(struct T, d);
       case 2: return offsetof(struct T, c);
       case 3: return offsetof(struct T, s);
       case 4: return offsetof(struct T, f);
       default: break;
       }
       return 0;
    }
    
    int main(void)
    {
       int i;
       for ( i = 0; i < 6; ++i )
       {
          printf("Toffset(&#37;d) = %d\n", i, Toffset(i));
       }
       return 0;
    }
    
    /* my output
    Toffset(0) = 0
    Toffset(1) = 8
    Toffset(2) = 16
    Toffset(3) = 17
    Toffset(4) = 28
    Toffset(5) = 0
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by matsp View Post
    Pedantisizm prevention: If we are usign C99, we probably should use %zd as the format specifier, as offsetof forms a size_t value, and that should use the z modifier to tell printf that it is of "size_t" size, rather than regular int.
    Pendant attack!

    d is for signed integrals, u is for unsigned integrals, so the correct C99 format specifier for size_t would be %zu. In strict C89, it would be better to use %lu and cast to unsigned long instead of using %d.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Pedantisizm accepted. However, it is HIGHLY unlikely that the offset within a struct is bigger than 2^n-1 where n is the number of bits in size_t. So signed or unsigned will make no difference.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Agreed, it likely wouldn't make a difference.

  9. #9
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by robwhit View Post
    Pendant attack!
    I hope we're not being attacked by necklaces! You mean pedant not pendant...

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by QuantumPete View Post
    I hope we're not being attacked by necklaces! You mean pedant not pendant...

    QuantumPete
    LOL!!! I didn't even see that 'n' there.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    no, I meant necklaces.

    they'll choke you, they will.

    edit: off to change my user title...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. offsetof
    By Dave_Sinkula in forum C Programming
    Replies: 2
    Last Post: 06-21-2003, 03:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21