473,732 Members | 2,175 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_ACTIO N (PLC_BASE)
#define PLC_FIELD_TOBRG (PLC_BASE +
SCHEDLINES*size of(fieldSchedul e_t.action)/PLC_UNIT)
#define PLC_FIELD_FROMB RG (PLC_FIELD_TOBR G +
SCHEDLINES*size of(fieldSchedul e_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_ACTI ON", PLC_FIELD_ACTIO N,
"PLC_FIELD_TOBR G", PLC_FIELD_TOBRG ,
"PLC_FIELD_FROM BRG", PLC_FIELD_FROMB RG);
}

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 6080


"CptDondo" <ya*@NsOeSiPnAe Mr.comwrote in message
news:12******** *****@corp.supe rnews.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_ACTIO N (PLC_BASE)
#define PLC_FIELD_TOBRG (PLC_BASE +
SCHEDLINES*size of(fieldSchedul e_t.action)/PLC_UNIT)
#define PLC_FIELD_FROMB RG (PLC_FIELD_TOBR G +
SCHEDLINES*size of(fieldSchedul e_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_ACTI ON", PLC_FIELD_ACTIO N,
"PLC_FIELD_TOBR G", PLC_FIELD_TOBRG ,
"PLC_FIELD_FROM BRG", PLC_FIELD_FROMB RG);
}

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.toB earing);

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*@NsOeSiPnAe Mr.comwrote in message
news:12******** *****@corp.supe rnews.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_ACTIO N (PLC_BASE)
#define PLC_FIELD_TOBRG (PLC_BASE +
SCHEDLINES*siz eof(fieldSchedu le_t.action)/PLC_UNIT)
#define PLC_FIELD_FROMB RG (PLC_FIELD_TOBR G +
SCHEDLINES*siz eof(fieldSchedu le_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_ACTI ON", PLC_FIELD_ACTIO N,
"PLC_FIELD_TOBR G", PLC_FIELD_TOBRG ,
"PLC_FIELD_FROM BRG", PLC_FIELD_FROMB RG);
}

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.toB earing);

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(fieldSch edule_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(((stru ct 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*@NsOeSiPnAe Mr.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_ACTIO N (PLC_BASE)
#define PLC_FIELD_TOBRG (PLC_BASE +
SCHEDLINES*size of(fieldSchedul e_t.action)/PLC_UNIT)
#define PLC_FIELD_FROMB RG (PLC_FIELD_TOBR G +
SCHEDLINES*size of(fieldSchedul e_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_Keit h) 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.toB earing);

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
8876
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 expression should equal to "sizeof( (int)(*p) )", but the compiler does NOT think so. Why? Can anyone help me? Thanks. Best regards. Roy
3
3553
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 know it returns two bytes in UniCode. Please help... For ANSI: In ISO/ANSI C++ Standard, 5.3.3 § 1, it stays: "The sizeof operator yields the number of bytes in the object representation of its
12
2740
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
3403
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 <offset>). What I would like to do is create an API something like this: #include <stddef.h> struct MemMap { unsigned char apple; // 8 bits on my platform
18
12958
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
2006
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? Thanks
11
1589
by: Richard Tobin | last post by:
Please excuse me if this has already been covered. Given char x; is sizeof(x)
7
1968
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. The original post at gnu.gcc.help can be found at this link http://groups.google.com/group/gnu.gcc.help/browse_thread/thread/ece55cbbd9c36270/2148a6c1ac6119e1?lnk=st&q=#2148a6c1ac6119e1 Here's the question: class base {
27
5601
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 --
0
8944
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8773
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9234
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6733
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4548
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4805
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2177
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.