unions with structures of common initial sequence

Discussion in 'C Programming' started by aarklon@gmail.com, Apr 1, 2006.

  1. Guest

    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
    , Apr 1, 2006
    #1
    1. Advertising

  2. Michael Mair Guest

    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.
    Michael Mair, Apr 1, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neil Zanella
    Replies:
    9
    Views:
    416
    Jeffrey D. Smith
    Oct 16, 2003
  2. Alfonso Morra
    Replies:
    11
    Views:
    709
    Emmanuel Delahaye
    Sep 24, 2005
  3. Jason Curl

    Unions and structures implementation in C

    Jason Curl, Oct 12, 2005, in forum: C Programming
    Replies:
    4
    Views:
    797
    Default User
    Oct 12, 2005
  4. Replies:
    20
    Views:
    701
    Richard
    Aug 10, 2007
  5. Morris Dovey

    Unions in structures

    Morris Dovey, Mar 16, 2008, in forum: C Programming
    Replies:
    9
    Views:
    384
    Eric Sosman
    Mar 17, 2008
Loading...

Share This Page