Query abt union

S

sumit.sharma

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
 
E

Ed Morton

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); }
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.
 
P

Peter Shaggy Haywood

Groovy hepcat (e-mail address removed) 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"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top