Sizeof typdef'ed structures

Discussion in 'C Programming' started by Ann O'Nymous, Oct 15, 2010.

  1. Ann O'Nymous

    Ann O'Nymous Guest

    How do I do this:


    typedef struct foobar {
    int a;
    char b[16];
    float c;
    } my_struct;

    #define my_structsize sizeof(struct foobar)



    I just want to define my_structsize to be the size of the structure
    defined in the typdef (one int+16 chars+1 float)

    This is just a syntax issue where I can't seem to come up with the right
    combination (tried sizeof(my_struct) and several combos)
     
    Ann O'Nymous, Oct 15, 2010
    #1
    1. Advertising

  2. Ann O'Nymous

    Seebs Guest

    On 2010-10-15, Ann O'Nymous <> wrote:
    > How do I do this:


    > typedef struct foobar {
    > int a;
    > char b[16];
    > float c;
    > } my_struct;


    > #define my_structsize sizeof(struct foobar)


    Well, you just did it.

    > I just want to define my_structsize to be the size of the structure
    > defined in the typdef (one int+16 chars+1 float)


    Plus possible padding -- although on most systems I don't think you'd see
    any here.

    > This is just a syntax issue where I can't seem to come up with the right
    > combination (tried sizeof(my_struct) and several combos)


    And you also can't seem to tell us what isn't working about this. I
    think it might help if you showed us a failing program which tries to
    use the my_structsize define.

    e.g., if I use the above, then write:

    int main(void) { printf("%d\n", (int) my_structsize); return 0; }

    I get a program which prints "24\n", which seems like it's working.

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    I am not speaking for my employer, although they do rent some of my opinions.
     
    Seebs, Oct 15, 2010
    #2
    1. Advertising

  3. Ann O'Nymous

    Ben Pfaff Guest

    Ann O'Nymous <> writes:

    > typedef struct foobar {
    > int a;
    > char b[16];
    > float c;
    > } my_struct;
    >
    > #define my_structsize sizeof(struct foobar)
    >
    >
    >
    > I just want to define my_structsize to be the size of the structure
    > defined in the typdef (one int+16 chars+1 float)


    What you have there will work, although you can drop the
    "typedef" and "my_struct" parts.
    --
    Ben Pfaff
    http://benpfaff.org
     
    Ben Pfaff, Oct 15, 2010
    #3
  4. Ann O'Nymous

    Ann O'Nymous Guest

    On 10/15/2010 5:18 PM, Seebs wrote:
    > On 2010-10-15, Ann O'Nymous<> wrote:
    >> How do I do this:

    >
    >> typedef struct foobar {
    >> int a;
    >> char b[16];
    >> float c;
    >> } my_struct;

    >
    >> #define my_structsize sizeof(struct foobar)

    >
    > Well, you just did it.


    OK, it's a macro that uses "my_structsize" that's blowing up. It
    complains that "sizeof(struct foobar)" is not an lvalue but is used in a
    context that requires one. I have to investigate the macro.

    Another statement accepted it without error, had I noticed that, I
    wouldn't have blamed the sizeof().
     
    Ann O'Nymous, Oct 15, 2010
    #4
  5. Ann O'Nymous

    Ian Collins Guest

    On 10/16/10 10:51 AM, Ann O'Nymous wrote:
    > On 10/15/2010 5:18 PM, Seebs wrote:
    >> On 2010-10-15, Ann O'Nymous<> wrote:
    >>> How do I do this:

    >>
    >>> typedef struct foobar {
    >>> int a;
    >>> char b[16];
    >>> float c;
    >>> } my_struct;

    >>
    >>> #define my_structsize sizeof(struct foobar)

    >>
    >> Well, you just did it.

    >
    > OK, it's a macro that uses "my_structsize" that's blowing up. It
    > complains that "sizeof(struct foobar)" is not an lvalue but is used in a
    > context that requires one. I have to investigate the macro.


    There's nothing wrong with the macro, you appear to be using it wrong.
    I say appear because you haven't posted the offending code, please do!

    --
    Ian Collins
     
    Ian Collins, Oct 15, 2010
    #5
  6. Ann O'Nymous

    Seebs Guest

    On 2010-10-15, Ann O'Nymous <> wrote:
    > OK, it's a macro that uses "my_structsize" that's blowing up. It
    > complains that "sizeof(struct foobar)" is not an lvalue but is used in a
    > context that requires one. I have to investigate the macro.


    Ah-hah!

    > Another statement accepted it without error, had I noticed that, I
    > wouldn't have blamed the sizeof().


    From this we derive the golden rule of debugging:

    Try to build a smaller reproducer. :)

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    I am not speaking for my employer, although they do rent some of my opinions.
     
    Seebs, Oct 16, 2010
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Piffer

    typdef'ing from sig_atomic_t valid?

    Mark Piffer, Apr 22, 2004, in forum: C Programming
    Replies:
    21
    Views:
    1,906
    Dan Pop
    Apr 29, 2004
  2. baumann@pan
    Replies:
    42
    Views:
    1,033
  3. Alfonso Morra
    Replies:
    11
    Views:
    722
    Emmanuel Delahaye
    Sep 24, 2005
  4. Johan Tibell

    Typdef pointers to structs or not?

    Johan Tibell, Jul 27, 2006, in forum: C Programming
    Replies:
    17
    Views:
    549
  5. Mark  Snelling

    How do you typdef a function template

    Mark Snelling, Oct 24, 2005, in forum: C++
    Replies:
    14
    Views:
    456
    Kai-Uwe Bux
    Oct 25, 2005
Loading...

Share This Page