469,601 Members | 2,308 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,601 developers. It's quick & easy.

Query abt union

Hi,

I have some experience in C prog language. I have small doubt abt
using unions in C language.

I have the foll. union

union MyUnion
{
char name [100];
double dblval;
int intVal;
};

Since all the members of union share the same memory space, if we set
the value of one union members, it overwrites the other field's value.
How do the OS maintain that a particular member is set and not the
other member.

Say like, I use

MyUnion ux;

strcpy(ux.name, "C Language"); /* name is set */

/* try to overwrite name field */

ux.intVal = 0xFF6677;

Now at this time how does OS maintains that intVal is now used... !!!!

Also, if I now try to print name field, what it shld be ??

Pls reply in ur FREE time..there is no urgency..

sumit
Nov 13 '05 #1
2 3439
> Hi,

I have some experience in C prog language. I have small doubt abt
using unions in C language.

I have the foll. union

union MyUnion
{
char name [100];
double dblval;
int intVal;
};

Since all the members of union share the same memory space, if we set
the value of one union members, it overwrites the other field's value.
How do the OS maintain that a particular member is set and not the
other member.
It doesn't.
Say like, I use

MyUnion ux;

strcpy(ux.name, "C Language"); /* name is set */

/* try to overwrite name field */

ux.intVal = 0xFF6677;

Now at this time how does OS maintains that intVal is now used... !!!!
Because you just told it (ux.intVal = ...).
Also, if I now try to print name field, what it shld be ??


Try running this program:

#include <stdio.h>
#include <string.h>

typedef union {
char name [20];
double dblval;
int intVal;
} MyUnion;

static MyUnion ux;

void prtName() {
int i;
for (i=0; i < sizeof ux.name; i++) { putchar(ux.name[i]); }
putchar('\n');
}

int
main () {
strcpy(ux.name, "C Language"); /* name is set */
prtName();
ux.intVal = 0xFF6677;
prtName();
}

and you'll see the "C L" at the start of "C Language" as originally stored in
ux.name now being replaced by more interesting characters dervied from 0xFF6677.

Ed.

Nov 13 '05 #2
Groovy hepcat su**********@wipro.com was jivin' on 14 Jul 2003
10:50:56 -0700 in comp.lang.c.
Query abt union's a cool scene! Dig it!
Since all the members of union share the same memory space, if we set
the value of one union members, it overwrites the other field's value.
How do the OS maintain that a particular member is set and not the
other member.


The OS (operating system) has absolutely nothing whatsoever to do
with it. If you don't keep track of what you store in a union, then
nothing will.
It is very important that you do keep track, because taking the
value of a union member which was not the last one modified causes
undefined behaviour.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Nov 13 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by techquest | last post: by
3 posts views Thread by Paradigm | last post: by
2 posts views Thread by mattytee123 | last post: by
7 posts views Thread by KoliPoki | last post: by
4 posts views Thread by Stan | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.