Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > unions with structures of common initial sequence

Reply
Thread Tools

unions with structures of common initial sequence

 
 
aarklon@gmail.com
Guest
Posts: n/a
 
      04-01-2006
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

 
Reply With Quote
 
 
 
 
Michael Mair
Guest
Posts: n/a
 
      04-01-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) schrieb:
> 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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unions in structures Morris Dovey C Programming 9 03-17-2008 06:11 PM
byte alignment in structures and unions anon.asdf@gmail.com C Programming 20 08-10-2007 04:13 PM
Unions and structures implementation in C Jason Curl C Programming 4 10-12-2005 04:35 PM
structures, structures and more structures (questions about nestedstructures) Alfonso Morra C Programming 11 09-24-2005 07:42 PM
coding conventions: pointers, structures, and unions Neil Zanella C Programming 9 10-16-2003 02:47 AM



Advertisments