By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,986 Members | 2,030 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,986 IT Pros & Developers. It's quick & easy.

sizeof struct_t.member

P: n/a
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
Sep 14 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a


"CptDondo" <ya*@NsOeSiPnAeMr.comwrote in message
news:12*************@corp.supernews.com...
>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.
Sep 14 '06 #2

P: n/a
Malcolm wrote:
"CptDondo" <ya*@NsOeSiPnAeMr.comwrote in message
news:12*************@corp.supernews.com...
>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
Sep 14 '06 #3

P: n/a
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)

Sep 14 '06 #4

P: n/a
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?
Sep 14 '06 #5

P: n/a
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.

Sep 14 '06 #6

P: n/a
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
Sep 14 '06 #7

P: n/a
CptDondo <ya*@NsOeSiPnAeMr.comwrites:
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) ks***@mib.org <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.
Sep 14 '06 #8

P: n/a
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:Th*************@gmail.com>
Sep 15 '06 #9

P: n/a
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
Sep 15 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.