sizeof struct_t.member

C

CptDondo

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
 
M

Malcolm

CptDondo said:
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.
 
C

CptDondo

Malcolm said:
CptDondo said:
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
 
G

Guest

CptDondo said:
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)
 
C

CptDondo

Harald said:
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?
 
G

Guest

CptDondo said:
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.
 
C

CptDondo

Harald said:
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
 
K

Keith Thompson

CptDondo said:
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).
 
K

Kenneth Brody

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:[email protected]>
 
F

Frederick Gotham

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;
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top