# C Programming.. Typecasting of structure pointers..

**URL:** <https://community.unix.com/t/c-programming-typecasting-of-structure-pointers/315946>\
**Category:** Programming\
**Created:** [August 30, 2012, 1:37am UTC](https://community.unix.com/t/c-programming-typecasting-of-structure-pointers/315946 "2012-08-30T01:37:37Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Priya\_Amaresh](https://community.unix.com/letter_avatar/priya_amaresh/32/6_449c533f586939b6e5f246632a46421f.png) [@Priya\_Amaresh](https://community.unix.com/u/Priya_Amaresh)\
**Post date:** [August 30, 2012, 1:37am UTC](https://community.unix.com/t/c-programming-typecasting-of-structure-pointers/315946/1 "2012-08-30T01:37:37Z")

</div>

Hi,  
I need to pass a typecasted structure pointer of one structure to another structure pointer..  
The parameters of both structures are different say if first structure has int, char datatypes and second structure has float, double,char..  
Please help.. I need to get the values of one structure and pass them to another structure..  
Its pretty confusing me..:eek:

---

<div class="post-metadata">

**Author:** ![Don\_Cragun](https://community.unix.com/user_avatar/community.unix.com/don_cragun/32/2682_2.png) [@Don\_Cragun](https://community.unix.com/u/Don_Cragun)\
**Post date:** [August 30, 2012, 6:51am UTC](https://community.unix.com/t/c-programming-typecasting-of-structure-pointers/315946/2 "2012-08-30T06:51:10Z")

</div>

It sounds like you need a single structure containing a union rather than multiple structures. If you can show us details about what structures you have and what you're trying to get out of them, we might be able to help more.

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_449c533f586939b6e5f246632a46421f.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [August 30, 2012, 11:39am UTC](https://community.unix.com/t/c-programming-typecasting-of-structure-pointers/315946/3 "2012-08-30T11:39:35Z")

</div>

Yes, what you usually do is something like this:

```nohighlight
#include <stdio.h>

typedef struct type_float {
        int type;
        float payload;
} type_float;

typedef struct type_string {
        int type;
        char payload[64];
} type_string;

typedef union alltypes {
        /* An enum is like an integer, plus a bunch of #define's.
            TYPE_FLOAT becomes 0, TYPE_STRING becomes 1, etc, etc. */
        enum { TYPE_FLOAT, TYPE_STRING } type;
        type_float t_float;
        type_string t_string;
} alltypes;

int dosomething(alltypes *a)
{
        switch(a->type)
        {
        case TYPE_FLOAT:
                printf("TYPE_FLOAT: %f\n", a->t_float.payload);
                break;
        case TYPE_STRING:
                printf("TYPE_STRING: %s\n", a->t_string.payload);
                break;
        default:
                printf("Wrong type '%d'\n", a->type);
                return(-1);
                break;
        }

        return(0);
}

int main(int argc, char *argv[])
{
        type_float tf={TYPE_FLOAT, 3.14159 };
        type_string ts={TYPE_STRING, "HEY GUYS"};
        dosomething((alltypes *)&tf);
        dosomething((alltypes *)&ts);
        return(0);
}

```

Note that a union actually stores all elements in the same place, so its members t\_float, t\_string and type actually start at the same address. They all share their type variable in common. (Wherever the held-in-common variable is used, I highlight in red.) So you can pass different types of structures, and have the first value define which type it gets used as, without lots of different kinds of messy typecasts.

---

<div class="post-metadata">

**Author:** ![Priya\_Amaresh](https://community.unix.com/letter_avatar/priya_amaresh/32/6_449c533f586939b6e5f246632a46421f.png) [@Priya\_Amaresh](https://community.unix.com/u/Priya_Amaresh)\
**Post date:** [August 31, 2012, 4:32am UTC](https://community.unix.com/t/c-programming-typecasting-of-structure-pointers/315946/4 "2012-08-31T04:32:32Z")

</div>

Thanks a lot.. This was the exact thing i was searching to do.. It really worked.. 😃
