C Programming.. Typecasting of structure pointers..


 
Thread Tools Search this Thread
Top Forums Programming C Programming.. Typecasting of structure pointers..
# 1  
Old 08-30-2012
C Programming.. Typecasting of structure pointers..

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..Smilie
# 2  
Old 08-30-2012
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.
# 3  
Old 08-30-2012
Yes, what you usually do is something like this:

Code:
#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.
This User Gave Thanks to Corona688 For This Post:
# 4  
Old 08-31-2012
Thanks a lot.. This was the exact thing i was searching to do.. It really worked.. Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Pointers and array

Hello, I read from a book exercise for a challenge. How to print out each letter of char array a by two different pointers pa and ppa in the example? I have tried my code for letter "r" by testing without full understanding as only the first one worked. #include<stdio.h> int main() { char... (17 Replies)
Discussion started by: yifangt
17 Replies

2. Shell Programming and Scripting

Typecasting problem??

echo $verbiage "Nigeria","Can Nigeria succeed in their quest for a top 4 finish? Get daily Nigeria news, GOALS and LIVE Match updates for 30days at N200.Click Accept to SUBSCRIBE" but when i run this command sed 's/$/",$verbiage"/g' noverb.F5 I get below o/p ... (6 Replies)
Discussion started by: nikhil jain
6 Replies

3. Programming

Pointer to pointers

Hi guys, I'm trying to understand pointers in C and made a simple example and I've problems with It. Can someone help? #include <stdio.h> #include <stdlib.h> #include <assert.h> int f1(char **str_); int main(int argc, char **argv) { char *str = NULL; f1(&str); ... (3 Replies)
Discussion started by: pharaoh
3 Replies

4. Programming

Need help with the Pointers in C

I have a special character called ô. When it is declared as a character variable its showing it can be printed. But when it is declared as a character pointer variable its showing it cannot be printed. I am just wondering why its happening like this.. c1 = '@'; c2 = 'ô'; char *fp; fp="XXô"; if... (1 Reply)
Discussion started by: sivakumar.rj
1 Replies

5. Programming

Search attributes in one structure using the values from another structure

Hello Groups I am trying to find out ways of comparing a value from a 'c' structure to a value in another 'C' structure. the 'C' structure can be a List or liked list as it contains lot many records. if we loop it in both the structures it is going to consume time. I am looking for a simple... (3 Replies)
Discussion started by: dhanamurthy
3 Replies

6. Programming

restricted pointers

Hi all. I am trying to use restricted pointers to allow the gcc compiler optimize the code, but I have not been able to make it work so far. I am testing with this code: #include <stdlib.h> #include <stdio.h> #include <time.h> #include <sys/time.h> void vecmult(int n, int * restrict a, int... (0 Replies)
Discussion started by: carl.alv
0 Replies

7. Programming

pointers

Hi I mash with pointers in C. I solve this problem about 5 hours and I don't know how I should continue. void InsertFirst (tList *L, int val) { tElemPtr new; if((new = malloc(sizeof(tElemPtr))) == NULL) Error(); new->data = val; new->ptr = L->frst; L->frst = new;... (2 Replies)
Discussion started by: Milla
2 Replies

8. UNIX for Advanced & Expert Users

MV files from one directory structure(multiple level) to other directory structure

Hi, I am trying to write a script that will move all the files from source directory structure(multiple levels might exist) to destination directory structure. If a sub folder is source doesnot exist in destination then I have to skip and goto next level. I also need to delete the files in... (4 Replies)
Discussion started by: srmadab
4 Replies

9. Programming

pointers

is this a valid c declaration int (*ptr(int *b)); plz explain... (4 Replies)
Discussion started by: areef4u
4 Replies

10. UNIX for Dummies Questions & Answers

Copying a Directory Structure to a new structure

Hi all Is it possible to copy a structure of a directory only. e.g. I have a file with the following entries that is a result of a find :- /dir1/dir2/file.dbf /dir1/dir2/dir3/file1.dbf /dir1/file.dbf I want to copy these to a directory and keep the structure however starting at a new dir... (8 Replies)
Discussion started by: jhansrod
8 Replies
Login or Register to Ask a Question