sizeof struct_t.member

Discussion in 'C Programming' started by CptDondo, Sep 14, 2006.

  1. CptDondo

    CptDondo Guest

    I am missing something about structure declarations....

    I am trying to get the size of a structure member using sizeof.

    my xml.h file (beware of line wrap):

    struct fieldSchedule_t {
    uint8_t action;
    uint16_t fromBearing, toBearing;
    };

    my plc.h file:

    #define PLC_FIELD_ACTION (PLC_BASE)
    #define PLC_FIELD_TOBRG (PLC_BASE +
    SCHEDLINES*sizeof(fieldSchedule_t.action)/PLC_UNIT)
    #define PLC_FIELD_FROMBRG (PLC_FIELD_TOBRG +
    SCHEDLINES*sizeof(fieldSchedule_t.toBearing)/PLC_UNIT)

    my .c file:

    #include "xml.h"

    #include "plc.h"

    main() {

    printf("%s %d\n%s %d\n%s %d\n",
    "PLC_FIELD_ACTION", PLC_FIELD_ACTION,
    "PLC_FIELD_TOBRG", PLC_FIELD_TOBRG,
    "PLC_FIELD_FROMBRG", PLC_FIELD_FROMBRG);
    }

    The way I understand this, I should be able to get the size of the
    member action from the "structure tag" in the declaration.

    Alas it isn't so:

    [i386]yan@craywb:/home/local/panel/src/xml2plc$ gcc test.c
    test.c: In function `main':
    test.c:10: error: `fieldSchedule_t' undeclared (first use in this function)
    test.c:10: error: (Each undeclared identifier is reported only once
    test.c:10: error: for each function it appears in.)

    Is there some way to get the size of a structure member without actually
    allocating space for that structure?

    --Yan
     
    CptDondo, Sep 14, 2006
    #1
    1. Advertising

  2. CptDondo

    Malcolm Guest

    "CptDondo" <> wrote in message
    news:...
    >I am missing something about structure declarations....
    >
    > I am trying to get the size of a structure member using sizeof.
    >
    > my xml.h file (beware of line wrap):
    >
    > struct fieldSchedule_t {
    > uint8_t action;
    > uint16_t fromBearing, toBearing;
    > };
    >
    > my plc.h file:
    >
    > #define PLC_FIELD_ACTION (PLC_BASE)
    > #define PLC_FIELD_TOBRG (PLC_BASE +
    > SCHEDLINES*sizeof(fieldSchedule_t.action)/PLC_UNIT)
    > #define PLC_FIELD_FROMBRG (PLC_FIELD_TOBRG +
    > SCHEDLINES*sizeof(fieldSchedule_t.toBearing)/PLC_UNIT)
    >
    > my .c file:
    >
    > #include "xml.h"
    >
    > #include "plc.h"
    >
    > main() {
    >
    > printf("%s %d\n%s %d\n%s %d\n",
    > "PLC_FIELD_ACTION", PLC_FIELD_ACTION,
    > "PLC_FIELD_TOBRG", PLC_FIELD_TOBRG,
    > "PLC_FIELD_FROMBRG", PLC_FIELD_FROMBRG);
    > }
    >
    > The way I understand this, I should be able to get the size of the member
    > action from the "structure tag" in the declaration.
    >
    > Alas it isn't so:
    >
    > [i386]yan@craywb:/home/local/panel/src/xml2plc$ gcc test.c
    > test.c: In function `main':
    > test.c:10: error: `fieldSchedule_t' undeclared (first use in this
    > function)
    > test.c:10: error: (Each undeclared identifier is reported only once
    > test.c:10: error: for each function it appears in.)
    >
    > Is there some way to get the size of a structure member without actually
    > allocating space for that structure?
    >

    Horrid code.
    I'm glad the compiler choked on it.

    There is no nice way of getting the size of a structure member. If it
    happens to be a bitfield then it is impossible.

    struct fieldSchedule_t temp;

    sizeof(temp.toBearing);

    will do the trick, as long as you have an instance. Without an instance you
    are in the woods.

    --
    www.personal.leeds.ac.uk/~bgy1mm
    freeware games to download.
     
    Malcolm, Sep 14, 2006
    #2
    1. Advertising

  3. CptDondo

    CptDondo Guest

    Malcolm wrote:
    > "CptDondo" <> wrote in message
    > news:...
    >> I am missing something about structure declarations....
    >>
    >> I am trying to get the size of a structure member using sizeof.
    >>
    >> my xml.h file (beware of line wrap):
    >>
    >> struct fieldSchedule_t {
    >> uint8_t action;
    >> uint16_t fromBearing, toBearing;
    >> };
    >>
    >> my plc.h file:
    >>
    >> #define PLC_FIELD_ACTION (PLC_BASE)
    >> #define PLC_FIELD_TOBRG (PLC_BASE +
    >> SCHEDLINES*sizeof(fieldSchedule_t.action)/PLC_UNIT)
    >> #define PLC_FIELD_FROMBRG (PLC_FIELD_TOBRG +
    >> SCHEDLINES*sizeof(fieldSchedule_t.toBearing)/PLC_UNIT)
    >>
    >> my .c file:
    >>
    >> #include "xml.h"
    >>
    >> #include "plc.h"
    >>
    >> main() {
    >>
    >> printf("%s %d\n%s %d\n%s %d\n",
    >> "PLC_FIELD_ACTION", PLC_FIELD_ACTION,
    >> "PLC_FIELD_TOBRG", PLC_FIELD_TOBRG,
    >> "PLC_FIELD_FROMBRG", PLC_FIELD_FROMBRG);
    >> }
    >>
    >> The way I understand this, I should be able to get the size of the member
    >> action from the "structure tag" in the declaration.
    >>
    >> Alas it isn't so:
    >>
    >> [i386]yan@craywb:/home/local/panel/src/xml2plc$ gcc test.c
    >> test.c: In function `main':
    >> test.c:10: error: `fieldSchedule_t' undeclared (first use in this
    >> function)
    >> test.c:10: error: (Each undeclared identifier is reported only once
    >> test.c:10: error: for each function it appears in.)
    >>
    >> Is there some way to get the size of a structure member without actually
    >> allocating space for that structure?
    >>

    > Horrid code.
    > I'm glad the compiler choked on it.


    Inquiring minds want to know. What is horrid about it?

    (Besides the fact that the #define is pretty ugly; but I need to
    calculate those locations - and there's no need to do so at runtime.
    Although it may be cleaner.)

    >
    > There is no nice way of getting the size of a structure member. If it
    > happens to be a bitfield then it is impossible.
    >
    > struct fieldSchedule_t temp;
    >
    > sizeof(temp.toBearing);
    >
    > will do the trick, as long as you have an instance. Without an instance you
    > are in the woods.
    >


    OK, that's what I figured, but it makes the code even uglier.

    --Yan
     
    CptDondo, Sep 14, 2006
    #3
  4. CptDondo wrote:
    > I am missing something about structure declarations....
    >
    > I am trying to get the size of a structure member using sizeof.
    >
    > my xml.h file (beware of line wrap):
    >
    > struct fieldSchedule_t {
    > uint8_t action;
    > uint16_t fromBearing, toBearing;
    > };

    [...]
    > sizeof(fieldSchedule_t.action)

    [...]
    > [i386]yan@craywb:/home/local/panel/src/xml2plc$ gcc test.c
    > test.c: In function `main':
    > test.c:10: error: `fieldSchedule_t' undeclared (first use in this function)
    > test.c:10: error: (Each undeclared identifier is reported only once
    > test.c:10: error: for each function it appears in.)
    >
    > Is there some way to get the size of a structure member without actually
    > allocating space for that structure?


    It's not exactly pretty, but:

    sizeof(((struct fieldSchedule_t *) 0)->action)
     
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=, Sep 14, 2006
    #4
  5. CptDondo

    CptDondo Guest

    Harald van Dijk wrote:

    >> Is there some way to get the size of a structure member without actually
    >> allocating space for that structure?

    >
    > It's not exactly pretty, but:
    >
    > sizeof(((struct fieldSchedule_t *) 0)->action)
    >


    Let's see if I understand that:

    (struct fieldSchedule_t *) creates a pointer to a null

    then we pull the member from that null structure?

    Is that right?
     
    CptDondo, Sep 14, 2006
    #5
  6. CptDondo wrote:
    > Harald van Dijk wrote:
    >
    > >> Is there some way to get the size of a structure member without actually
    > >> allocating space for that structure?

    > >
    > > It's not exactly pretty, but:
    > >
    > > sizeof(((struct fieldSchedule_t *) 0)->action)
    > >

    >
    > Let's see if I understand that:
    >
    > (struct fieldSchedule_t *) creates a pointer to a null
    >
    > then we pull the member from that null structure?
    >
    > Is that right?


    Your wording is questionable at best, I think (the idea of a null
    pointer is that it doesn't point to anything), but other than that,
    pretty much.
     
    =?utf-8?B?SGFyYWxkIHZhbiBExLNr?=, Sep 14, 2006
    #6
  7. CptDondo

    CptDondo Guest

    Harald van Dijk wrote:

    >>>
    >>> sizeof(((struct fieldSchedule_t *) 0)->action)
    >>>

    >> Let's see if I understand that:
    >>
    >> (struct fieldSchedule_t *) creates a pointer to a null
    >>
    >> then we pull the member from that null structure?
    >>
    >> Is that right?

    >
    > Your wording is questionable at best, I think (the idea of a null
    > pointer is that it doesn't point to anything), but other than that,
    > pretty much.
    >


    :) I don't know if the english language can express the full range of
    C grammar.

    Thanks, it works like a charm.

    --Yan
     
    CptDondo, Sep 14, 2006
    #7
  8. CptDondo <> writes:
    > I am missing something about structure declarations....
    >
    > I am trying to get the size of a structure member using sizeof.
    >
    > my xml.h file (beware of line wrap):
    >
    > struct fieldSchedule_t {
    > uint8_t action;
    > uint16_t fromBearing, toBearing;
    > };
    >
    > my plc.h file:
    >
    > #define PLC_FIELD_ACTION (PLC_BASE)
    > #define PLC_FIELD_TOBRG (PLC_BASE +
    > SCHEDLINES*sizeof(fieldSchedule_t.action)/PLC_UNIT)
    > #define PLC_FIELD_FROMBRG (PLC_FIELD_TOBRG +
    > SCHEDLINES*sizeof(fieldSchedule_t.toBearing)/PLC_UNIT)

    [snip]
    > [i386]yan@craywb:/home/local/panel/src/xml2plc$ gcc test.c
    > test.c: In function `main':
    > test.c:10: error: `fieldSchedule_t' undeclared (first use in this function)
    > test.c:10: error: (Each undeclared identifier is reported only once
    > test.c:10: error: for each function it appears in.)
    >
    > Is there some way to get the size of a structure member without
    > actually allocating space for that structure?


    Two problems.

    First, your declaration creates a type called "struct
    fieldSchedule_t". There is no type called "fieldSchedule_t". If your
    compiler doesn't complain about that, you're probably using a C++
    compiler.

    Second, the operand of sizeof is either an expression or a
    parenthesized type name. Even if fieldSchedule_t is a valid type name
    (say, if you declared it as a typedef), the "." operator's left
    operand must be an *expression* of some structure (or union) type; it
    can't be the type itself.

    Harald van D?k (sorry, my newsreader doesn't display his last name
    properly) posted a solution involving applying sizeof to the member of
    a member of a dereferenced null pointer. This works because the
    operand of sizeof is not evaluated (as long as it doesn't involve a
    variable length array).

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Sep 14, 2006
    #8
  9. Malcolm wrote:
    [...]
    > There is no nice way of getting the size of a structure member. If it
    > happens to be a bitfield then it is impossible.
    >
    > struct fieldSchedule_t temp;
    >
    > sizeof(temp.toBearing);
    >
    > will do the trick, as long as you have an instance. Without an instance you
    > are in the woods.


    Can't you do something like this:

    sizeof( ((struct fieldSchedule_t *)NULL)->toBearing )

    This works on my system, but I don't know if it's legal.

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:>
     
    Kenneth Brody, Sep 15, 2006
    #9
  10. Malcolm posted:

    > There is no nice way of getting the size of a structure member. If it
    > happens to be a bitfield then it is impossible.



    How about something like:

    typedef struct SomeStruct { int a; char b; } SomeStruct;

    SomeStruct Func(void) {}

    int main(void)
    {
    sizeof Func().member;
    }


    --

    Frederick Gotham
     
    Frederick Gotham, Sep 15, 2006
    #10
    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. Derek
    Replies:
    7
    Views:
    24,340
    Ron Natalie
    Oct 14, 2004
  2. Trevor

    sizeof(str) or sizeof(str) - 1 ?

    Trevor, Apr 3, 2004, in forum: C Programming
    Replies:
    9
    Views:
    635
    CBFalconer
    Apr 10, 2004
  3. Vinu
    Replies:
    13
    Views:
    1,430
    Lawrence Kirby
    May 12, 2005
  4. blufox

    sizeof( int ) != sizeof( void * )

    blufox, May 22, 2006, in forum: C Programming
    Replies:
    2
    Views:
    560
    Joe Smith
    May 22, 2006
  5. Alex Vinokur
    Replies:
    7
    Views:
    498
    Clark S. Cox III
    Aug 14, 2006
Loading...

Share This Page