473,804 Members | 3,312 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Union In C

Hi All,
have some experience in C prog language. I have small doubt abt
using unions in C language.
Here is a small programm in vc++:

union a{
int b;
char c;
}d;

int main () {
d.b=360;
d.c=1;
printf("%d %d ",d.b ,d.c,);
}

In union largest sized member memory is reserved by union .

but for a sake if i declare this way then i've seen the d.b value is
showing 257 in output window and if i declare d.b =255 or below 255
that then it is showing the what is last defined for d.c, means value
is =1 for all variables.
So Here for first time how the memory is overwritten?
Please let me know on this . I am looking forward to you .

Thanks
Chinmoy
Oct 6 '08 #1
2 4322
On Oct 6, 3:20*pm, chang <chinmoy.chitta ran...@gmail.co mwrote:
Hi All,
*have some experience in C prog language. I have small doubt abt
using unions in C language.
Here is a small programm in vc++:

union a{
* * * * *int b;
* * * * *char c;

}d;

int main () {
d.b=360;
d.c=1;
printf("%d %d ",d.b ,d.c,);

}

In union largest sized member memory is reserved by union .

but for a sake if i declare this way then i've *seen *the d.b value is
showing 257 in output window and if i declare d.b =255 or below 255
that then it is showing the what is last defined for d.c, means value
is =1 for all variables.
So Here for first time how the memory is overwritten?
Please let *me know on this . I am looking forward to you .
IIRC, you are only allowed to access the value of union member which
was last being assigned with a value. This is definitely unspecified
behaviour, if not undefined. Because the size of integer is not fixed.

Spade.
Oct 6 '08 #2
On Mon, 6 Oct 2008 03:20:13 -0700 (PDT),
chang <ch************ ******@gmail.co mwrote:
Hi All,
have some experience in C prog language. I have small doubt abt
using unions in C language.
Here is a small programm in vc++:

union a{
int b;
char c;
}d;

int main () {
d.b=360;
d.c=1;
printf("%d %d ",d.b ,d.c,);
}
You can only access the value of the member you set last for a union.
So, if you set d.b, you can only read the value of d.b. Once you've
assigned something to d.c, you should no longer read the value of d.b
[1].

You also need to #include <stdio.hwhen you use printf().

Strictly speaking, the behaviour of the above program is undefined, and
therefore anything could happen.
In union largest sized member memory is reserved by union .
The size of the union is _at least_ the size of its largest member. It
can be larger.
but for a sake if i declare this way then i've seen the d.b value is
showing 257 in output window and if i declare d.b =255 or below 255
that then it is showing the what is last defined for d.c, means value
is =1 for all variables.
So Here for first time how the memory is overwritten?
Please let me know on this . I am looking forward to you .
next time, please simply write a program with inputs and outputs, rather
than trying to describe it in words. Programs and data are easier to
read and understand.

The members of a union share the same storage space. it is not entirely
clear from your post that you understand that. So, when you write to
one member, and then (incorrectly) access another member, the value that
you get back is potentially nonsensical. The system will try to
interpret the bit pattern in that location in a manner likely to be
incompatible with what was stored.

\begin{offtopic }

Assume your int is 16 bits (the same holds for a 32 bit int, but I don't
want to hape to type 16 extra zeroes.). The bit pattern for 360 would
be:

00000001:011010 00

Because your platform is little endian, that would live in memory like
this:

01101000:000000 01

Now you write to the char member of the union. The char is 8 bits, and
stored in the same _first_ 8 bits in the above pattern. You will store 1
in there, ending up with:

00000001:000000 01

Then you go and read this as an int again, ending up with the value 257.

Note that on a bigendian system the result will be wildly different,
which is one of the main reasons you need to not do this.

\end{offtopic}
Martien

[1] There are some special exceptions to this, but they do not apply
here.
--
|
Martien Verbruggen | Computers in the future may weigh no more
| than 1.5 tons. -- Popular Mechanics, 1949
|
Oct 6 '08 #3

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

Similar topics

5
8151
by: Simon Elliott | last post by:
I'd like to do something along these lines: struct foo { int i1_; int i2_; }; struct bar {
6
3335
by: Neil Zanella | last post by:
Hello, I would like to know what the C standards (and in particular the C99 standard) have to say about union initializers with regards to the following code snippet (which compiles fine under gcc 3.2.2 but does not produce the expected results, the expected results being the ones annotated in the comments in the code): #include <stdlib.h>
2
4360
by: Barry Schwarz | last post by:
Given a union of the form union { T1 m1; T2 m2;}obj; where T1 and T2 are different scalar (non-aggregate) types. The C99 standard states that obj.m1 = value; if (obj.m2 ... invokes undefined behavior because my reference to the union is via a
10
5079
by: Denis Pithon | last post by:
Hi, C lovers! I stuck on an union problem Here is snippet of my code .... /* two pointers of function with repsectively one and two argues */ typedef int (*dce_sn_f)(dce_t*);
2
5060
by: Peter Dunker | last post by:
Hi, I will write ANSI C89. Is the following struct defenition correct ? I wrote it with VC6(Windows IDE) and at first no Problem. As I changed a compiler switch to 'no language extension', the compiler said that the union has no name. Is it right that in ANSI C the union must be named inside this kind of structure ?
73
4075
by: Sean Dolan | last post by:
typedef struct ntt { int type; union { int i; char* s; }; }nt; nt n; n.i = 0;
18
2382
by: ranjeet.gupta | last post by:
Dear ALL As we know that when we declare the union then we have the size of the union which is the size of the highest data type as in the below case the size should be 4 (For my case and compiler), and it is, what I conclude from the below code union data_type {
30
3290
by: Yevgen Muntyan | last post by:
Hey, Why is it legal to do union U {unsigned char u; int a;}; union U u; u.a = 1; u.u; I tried to find it in the standard, but I only found that
5
3848
by: wugon.net | last post by:
question: db2 LUW V8 UNION ALL with table function month() have bad query performance Env: db2 LUW V8 + FP14 Problem : We have history data from 2005/01/01 ~ 2007/05/xx in single big table, we try separate this big table into twelve tables and create a view
3
7726
by: SRK | last post by:
Hi, I wanted to use an anonymous union within an structure something like below - struct Test { union { std::string user; //char user; std::string role; //char role;
0
9575
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,...
0
10320
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10073
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9134
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
7609
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
6846
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5645
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4288
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 we have to send another system
2
3806
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.