473,486 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

value assignment to a structure member inside the structure

1 New Member
Is it possible to assign a value to structure member inside the structure.like.....
Expand|Select|Wrap|Line Numbers
  1. struct control{
  2.  
  3. char tbi:2 =0;
  4. char res:1 =0;
  5. };
Oct 7 '14 #1
6 1363
donbock
2,426 Recognized Expert Top Contributor
Not like that. The struct command defines a new type. Putting the assignments in the struct command makes it look like you want these to be the initial values of those fields for every variable of this structure type. That sounds like a C++ constructor; but C doesn't have feature like that.
Oct 7 '14 #2
stdq
94 New Member
I just wanted to point that, as far as I know, you can't use the ':' character to name a variable.
Oct 7 '14 #3
Banfa
9,065 Recognized Expert Moderator Expert
@stdq - you'd be wrong because it is not naming a field it is declaring a bit field. Now bit fields are a bit platform dependant and really just syntactic sugar for using bit masks but they can be useful (particularly mapping data in microprocessor peripheral registers).

Effectively the code in the OP declares a field of 2 bits and a field of 1 bit, which will be followed by padding bits to bring the total size up to the number of bits in a char.

See http://en.wikipedia.org/wiki/Bit_field
Oct 7 '14 #4
donbock
2,426 Recognized Expert Top Contributor
In the OP code, the only initial values we see happen to be zero. If there are indeed no nonzero initial values then this effect can be achieved by declaring the structure variables with static duration. Variables with static duration are implicitly initialized to all zero before the first instruction in your program executes.

Static storage duration is any variable defined with the static keyword or any global variable (explicit or implicit extern keyword).
Oct 7 '14 #5
donbock
2,426 Recognized Expert Top Contributor
@Banfa - Standard C requires bit fields be of type unsigned int, signed int, int, or _Bool (C99 only). Notice that whether an int bit field is signed or unsigned is implementation-dependent -- quite different from an int variable, which is always signed.

I'm told that allowing other integer types (including char) for bit fields is a nonstandard extension found in some compilers. Needless to say, nonstandard extensions should be avoided.

Blast from the past: pre-Standard K&R C only supported bit fields of type unsigned.
Oct 7 '14 #6
Banfa
9,065 Recognized Expert Moderator Expert
@donbock you are of course correct on all counts and I probably should have pointed that out myself (I did think about it while I was typing if that counts for anything :D).

It is a relatively common non-standard extension so I assumed that is what was being used in this case (because actually that is the only thing that makes sense given the code).
Oct 8 '14 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

2
2769
by: B. Wood | last post by:
I have written a simple program the has a structure with two members. There are two strange things going on. 1. When on of the structure members is tested in a condition, the value of the...
4
4060
by: Aaron Walker | last post by:
I am trying to write this piece of code that reads a config file, in the format: ENTRY value and I want to read the config file, then read the "value" into the appropriate member of the...
1
5226
by: Dmitri Vorobiev | last post by:
Hello group, Is it possible to check the presence of a structure member at compile time using only the C preprocessor? What I have in mind could look like the following. Suppose the...
7
3411
by: Randy Yates | last post by:
Hi, I work in an embedded environment in which we often use a mix of C and assembly code. Thus a recurring requirement is to be able to take a C header file with structure definitions as input...
1
6404
by: Mikko Penkkimäki | last post by:
I have a struct in C#-project and C++-project. I use the struct in C# but it has to be filled in C++. I know that arrays work like this: In C++ code: struct teststruct { char str; float...
0
1032
by: JC Voon | last post by:
Hi All: I have a problem when trying to set the value of the structure member using reflaction,here is the code: Public Const MemberAccess As BindingFlags = BindingFlags.Public Or...
7
12356
by: raghunandan_1081 | last post by:
Hi guys, can you please tell me what is the Difference between c structure and c++ structure
2
2924
by: dobest03 | last post by:
Hi. Are there any way to access the integer member 'a' of outer structure from inner structure's member function func_inner()? See below the structure... Thanks. struct outer {
4
2540
by: sandeep | last post by:
Hi why we cannot have static as a structure member? & also is there any way to achive data hiding in C at this level( i.e. access only selected structure member ) following code gives syntax...
9
5412
by: cman | last post by:
What are the mechanics involved in the calculation of an offset of a structure member, demonstrated in this piece of code? #define list_entry(ptr, type, member) \ ((type *)((char...
0
7099
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
6964
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...
0
7123
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,...
1
6842
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
7319
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...
1
4864
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...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
262
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...

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.