473,385 Members | 1,357 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

sizeof struct_t.member

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
9 6055


"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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

70
by: Roy Yao | last post by:
Does it mean "(sizeof(int))* (p)" or "sizeof( (int)(*p) )" ? According to my analysis, operator sizeof, (type) and * have the same precedence, and they combine from right to left. Then this...
3
by: Sunil Menon | last post by:
Dear All, A class having no member variables and only a method sizeof(object) will return 1byte in ANSI and two bytes in Unicode. I have the answer for this of how in works in ANSI. But I don't...
12
by: Casper | last post by:
How would one go about summing up the memmory custom tree structures occupies in memmory? Example: struct node { struct node *parent; unsigned int nChildCount; string folder; //string class...
10
by: Mark A. Odell | last post by:
Is there a way to obtain the size of a struct element based only upon its offset within the struct? I seem unable to figure out a way to do this (short of comparing every element's offset with...
18
by: Mockey Chen | last post by:
My friend ask me a question as following: give a union SU define as: typedef union _SU { short x; struct y{ char a; short b; char c;
1
by: nicolas.gendron | last post by:
Is there a way to do this? I don't want to use sizeof(non_static_member_variable_type) because if the type of my member variable changes, the sizeof operator won't be informe. Any solution? ...
11
by: Richard Tobin | last post by:
Please excuse me if this has already been covered. Given char x; is sizeof(x)
7
by: Yen Kwoon | last post by:
Note: This problem is related to gcc but after some back and forth in group gnu.gcc.help it seems to have morph into more of a c++ specificiation question, hence the transplanting to this group. ...
27
by: CodeMonk3y | last post by:
gotta question on sizeof keyword does the sizeof keyword calcuates the size at compile time or run time ?? -- Posted on news://freenews.netfront.net - Complaints to news@netfront.net --
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.