unions with structures of common initial sequence

A

aarklon

Hi folks,

Recently i was reading the book C an advanced introduction by narain
gehani,

in page no:236 it is written as

If a union contains several structures with a common initial sequence,
then members
of this sequence are guaranteed to have the same values and they can be
accessed
via any of the structures after the union has been initialized via one
of the structures

now can anybody give examples for this.???

does it mean that a union is having several structures as its data
members,and these structures are having same type of data elements and
if we are initializing the union
all the structure members will be having same value
 
M

Michael Mair

Hi folks,

Recently i was reading the book C an advanced introduction by narain
gehani,

in page no:236 it is written as

If a union contains several structures with a common initial sequence,
then members
of this sequence are guaranteed to have the same values and they can be
accessed
via any of the structures after the union has been initialized via one
of the structures

now can anybody give examples for this.???

does it mean that a union is having several structures as its data
members,and these structures are having same type of data elements and
if we are initializing the union
all the structure members will be having same value

#define EXA_TYPE_NONE 0
#define EXA_TYPE_SIMPLEX 1
#define EXA_TYPE_BALL 2
#define EXA_TYPE_CART_INTERVALL 3

union exa {
struct {
unsigned long typedescriptor;
unsigned long size;
char *info;
} generic;
struct {
unsigned long typedescriptor;
unsigned long size;
char *info;
double corners[DIM+1][DIM];
} simplex;
struct {
unsigned long typedescriptor;
unsigned long size;
char *info;
double centre[DIM];
double radius;
} ball;
struct {
unsigned long typedescriptor;
unsigned long size;
char *info;
double bottomleftback[DIM];
double toprightfront[DIM];
} cartesianproductofintervals;
} example;

.....

switch (example.generic.typedescriptor) {
case EXA_TYPE_SIMPLEX:
/* access example.simplex */

....
}

.....
void copyInfo (union exa *dest, union exa *src)
{
unsigned long size = src->generic.size;
if (dest->generic.size >= size) {
strcpy(dest->generic.info, src->generic.info);
}
else {
char *tmp = realloc(dest->generic.info, size);
if (tmp) {
dest->generic.info = tmp;
dest->generic.size = size;
strcpy(dest->generic.info, src->generic.info);
}
else if (dest->generic.size >= strlen(src->generic.info)) {
strcpy(dest->generic.info, src->generic.info);
}
else {
strncpy(dest->generic.info, src->generic.info, size);
dest->generic.info[size-1]='\0';
RaiseWarning("Out of Memory: Truncated Info\n");
}
}

This is, of course, untested but should illustrate the point.


Cheers
Michael
 

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

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top