Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > sizeof struct_t.member

Reply
Thread Tools

sizeof struct_t.member

 
 
CptDondo
Guest
Posts: n/a
 
      09-14-2006
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
 
Reply With Quote
 
 
 
 
Malcolm
Guest
Posts: n/a
 
      09-14-2006




"CptDondo" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>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.


 
Reply With Quote
 
 
 
 
CptDondo
Guest
Posts: n/a
 
      09-14-2006
Malcolm wrote:
> "CptDondo" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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
 
Reply With Quote
 
=?utf-8?B?SGFyYWxkIHZhbiBExLNr?=
Guest
Posts: n/a
 
      09-14-2006
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)

 
Reply With Quote
 
CptDondo
Guest
Posts: n/a
 
      09-14-2006
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?
 
Reply With Quote
 
=?utf-8?B?SGFyYWxkIHZhbiBExLNr?=
Guest
Posts: n/a
 
      09-14-2006
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.

 
Reply With Quote
 
CptDondo
Guest
Posts: n/a
 
      09-14-2006
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
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      09-14-2006
CptDondo <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) <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.
 
Reply With Quote
 
Kenneth Brody
Guest
Posts: n/a
 
      09-15-2006
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: <(E-Mail Removed)>


 
Reply With Quote
 
Frederick Gotham
Guest
Posts: n/a
 
      09-15-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
sizeof(EmptyStruct) in C and C++ (was: Base {}; sizeof(Base) == 1?) Alex Vinokur C Programming 7 08-14-2006 04:57 PM
sizeof( int ) != sizeof( void * ) blufox C Programming 2 05-22-2006 03:25 PM
#define ARR_SIZE sizeof(arr)/sizeof(arr[0]) Vinu C Programming 13 05-12-2005 06:00 PM
sizeof(enum) == sizeof(int) ??? Derek C++ 7 10-14-2004 05:11 PM
sizeof(str) or sizeof(str) - 1 ? Trevor C Programming 9 04-10-2004 05:07 PM



Advertisments