473,406 Members | 2,208 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,406 software developers and data experts.

structure size

Hi.I have come across this structure which I could not understand.Could
anybody please explain this?

struct
{

int:a;
int:b;
char c;
double d;
}asd;

now what is int:a? Is it some way related with bits? the size of the
structure is 12 in linux systems.Thanks for help.
Eric

Aug 13 '06 #1
5 4549
di**********@yahoo.com wrote:
Hi.I have come across this structure which I could not understand.Could
anybody please explain this?

struct
{

int:a;
int:b;
char c;
double d;
}asd;

now what is int:a? Is it some way related with bits? the size of the
structure is 12 in linux systems.Thanks for help.
If `a' and `b' are constant integer expressions, both in a
reasonable range (non-negative and no greater than the number
of bits in an ordinary `int'), the above is legal but silly.
It says

- The variable `asd' is a struct with four elements.

- The first element is a nameless `int' that is `a' bits wide.

- The second is a nameless `int' that is `b' bits wide.

- The third is a `char' whose element name is `c'.

- The fourth is a `double' whose element name is `d'.

I cannot imagine a situation in which the two nameless
elements of this struct would serve a useful purpose, but my
imagination is less than infinite.

--
Eric Sosman
es*****@acm-dot-org.invalid
Aug 13 '06 #2
di**********@yahoo.com writes:
Hi.I have come across this structure which I could not understand.Could
anybody please explain this?

struct
{

int:a;
int:b;
char c;
double d;
}asd;

now what is int:a? Is it some way related with bits? the size of the
structure is 12 in linux systems.Thanks for help.
Are you *sure* that's what it says?

As Eric Sosman pointed out, "int:a;" is legal only if a is a constant
integer expression; it's then an anonymous bit field; a is the width
of the bit field, not its name. Most likely it would have to be a
macro, but macro names are traditionally written in all-caps. This
would have to be equivalent to:

struct {
int :8;
int :16;
char c;
double d;
} asd;

(Incidentally, this creates an anonymous struct type and a single
object of that type, which is rarely going to be useful.)

If the actual declaration is something like this:

struct {
int a:8;
int b:16;
char c;
double d;
} asd;

then a and b are *named* bit fields. (Beware: plain int bit fields
can be either signed or unsigned. You usually want bit fields to be
unsigned; if so, replace 'int" with "unsigned int".)

If you're asking about code you don't understand, you can very easily
lose vital details if you try to paraphrase it. That's why we
encourage people to copy-and-paste the *exact* actual code, so we
don't have to guess.

--
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.
Aug 13 '06 #3

Keith Thompson wrote:
di**********@yahoo.com writes:
Hi.I have come across this structure which I could not understand.Could
anybody please explain this?

struct
{

int:a;
int:b;
char c;
double d;
}asd;

now what is int:a? Is it some way related with bits? the size of the
structure is 12 in linux systems.Thanks for help.

Are you *sure* that's what it says?

As Eric Sosman pointed out, "int:a;" is legal only if a is a constant
integer expression; it's then an anonymous bit field; a is the width
of the bit field, not its name. Most likely it would have to be a
macro, but macro names are traditionally written in all-caps. This
would have to be equivalent to:

struct {
int :8;
int :16;
char c;
double d;
} asd;

(Incidentally, this creates an anonymous struct type and a single
object of that type, which is rarely going to be useful.)

If the actual declaration is something like this:

struct {
int a:8;
int b:16;
char c;
double d;
} asd;

then a and b are *named* bit fields. (Beware: plain int bit fields
can be either signed or unsigned. You usually want bit fields to be
unsigned; if so, replace 'int" with "unsigned int".)

If you're asking about code you don't understand, you can very easily
lose vital details if you try to paraphrase it. That's why we
encourage people to copy-and-paste the *exact* actual code, so we
don't have to guess.

--
struct
{

int:a;
int:b;
char c;
double d;
}asd;
Are you sure that it is a,b?Its already pointed out though.Now why is
the size of the variable asd reported to be 12? Its 8 for double,2 for
char.That makes 10.Now what about the rest 2?

Aug 13 '06 #4
"amit" <am*********@gmail.comwrites:
Keith Thompson wrote:
>di**********@yahoo.com writes:
[...]
struct
{

int:a;
int:b;
char c;
double d;
}asd;
Are you sure that it is a,b?Its already pointed out though.Now why is
the size of the variable asd reported to be 12? Its 8 for double,2 for
char.That makes 10.Now what about the rest 2?
Please quote properly; it's very hard to tell who wrote what here.

We still don't know how the structure is really declared. If it's
literally what was posted, we still need to see how a and b are
defined.

But see questions 2.12 and 2.13 of the FAQ, <http://www.c-faq.com/>.

--
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.
Aug 13 '06 #5
amit wrote:
[...]
Are you sure that it is a,b?Its already pointed out though.Now why is
the size of the variable asd reported to be 12? Its 8 for double,2 for
char.That makes 10.Now what about the rest 2?
Speculation is pointless until we see the actual code
and/or the definitions of `a' and `b'. As for the rest,
see Question 2.13 in the FAQ. (A review of Question 7.8
would also be a good idea.)

--
Eric Sosman
es*****@acm-dot-org.invalid
Aug 13 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: John | last post by:
Hi all, Can a linked list be a member of a structure? If so, when I add or remove an element from the linked list, the size of the structure will change. Will it cause any problem? Thanks a...
13
by: Amarendra | last post by:
Folks, This structure padding issue is bothering me now, could not locate a satisfactory answer on clc, so here it goes... I have a structure, given below: typedef struct { int flag; char...
2
by: Sachin | last post by:
typdef struct { int i; char ch; }str; str str_var; char x, y; main() { //do nothing
10
by: ranjeet.gupta | last post by:
Dear All !! Before i qoute my querry, I will like to qoute my analysis and my Knowledge Struct a { int raw; char data; };
4
by: marco_segurini | last post by:
Hi, From my VB program I call a C++ function that gets a structure pointer like parameter. The structure has a field that contains the structure length and other fields. My problem is that...
6
by: Laurent | last post by:
Hello, This is probably a dumb question, but I just would like to understand how the C# compiler computes the size of the managed structure or classes. I'm working on this class: public...
4
by: junky_fellow | last post by:
Can somebody please tell me about the structure alignment rules ? What I found was that on my system (cygwin running on PC, size of int=4 sizeof long=4, size of long long = 8) the cygwin compiler...
15
by: kris | last post by:
Hi I am writing a small program where I need to obtain the actual size of a structure. The programm is as follows struct abc { int j; char k; int i; }*a;
5
by: =?Utf-8?B?QXlrdXQgRXJnaW4=?= | last post by:
Hi Willy, Thank you very much for your work. C++ code doesnot make any serialization. So at runtime C# code gives an serialization error at "msg_file_s sa = (msg_file_s) bf.Deserialize(ms);"...
6
by: carles | last post by:
Hi, Here, sample code where a byte array is used to fill a particular structure: fs = File.OpenRead(path); // FileStream BITMAPFILEHEADER bfh = new BITMAPFILEHEADER(); b = new byte;
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
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...

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.