473,327 Members | 1,936 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,327 software developers and data experts.

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 4297
On Oct 6, 3:20*pm, chang <chinmoy.chittaran...@gmail.comwrote:
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.comwrote:
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:01101000

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

01101000:00000001

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:00000001

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
by: Simon Elliott | last post by:
I'd like to do something along these lines: struct foo { int i1_; int i2_; }; struct bar {
6
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...
2
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...
10
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
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...
73
by: Sean Dolan | last post by:
typedef struct ntt { int type; union { int i; char* s; }; }nt; nt n; n.i = 0;
18
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...
30
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
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...
3
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
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
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...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.