Like Tree2Likes
  • 2 Post By quzah

Casting of structs in union on char array

This is a discussion on Casting of structs in union on char array within the C Programming forums, part of the General Programming Boards category; Hi I have the following struct as below Code: typedef struct{ union{ typedef struct{ int b; }c; typedef struct{ int ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    31

    Casting of structs in union on char array

    Hi I have the following struct as below

    Code:
    typedef struct{
       union{
          typedef struct{
               int b;
          }c;
          typedef struct{
               int e;
          }f;
       }u;
    }a;
    I have a pointer declared

    Code:
    a* pter=(a*) malloc(sizeof(a));
    and a char buffer which I want to cast the struct c onto

    I am not sure how to go about doing this

    Code:
    pter=(a*)(c*)buffer;
    Or how do I access the struct c from the union?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't need to typecast the return from malloc. If you do, you either forgot a header or you are using C++ not C. On to your question: The same way you access any nested structure's members:
    Code:
    a *ptr = malloc( sizeof *ptr );
    ptr->u.c.b = 0xDEADBEEF;
    One has to wonder why you wrapped your union in a struct that contains nothing else.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    Aren't the two structs in the union basically the same?

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Ug. He's adding typedefs inside his union. So he didn't actually create instances of f and c, he just made types without actual variables. I discovered this when I was going to make a joke about "No, they're not the same, one is named f and one is named c!" My compiler weeps. It didn't error or warn, it just started crying.


    Quzah.
    ArunS and AndrewHunter like this.
    Hope is the first step on the road to disappointment.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Edelweiss View Post
    Hi I have the following struct as below
    Or how do I access the struct c from the union?
    That would be a.c.b ...

    What exactly are you trying to accomplish... not in code, in words...
    If you simply want to overlay a char array on an int you can do it like this...
    Code:
    union t_ctoi
      { unsigned char chs[sizeof(int)];
         int  num; } 
    ctoi;
    This will let you do stuff like this...
    Code:
    ctoi.num = 4242424242;
    printf("%c" ctoi.chs[2];

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    31
    Ops...sorry for giving a lousy example.

    Wanted to cast a struct from a union onto a char buffer and the structs in that union are supposed to have different variables.

    But I figured it out already. I initialized struct a and and access the struct which i wanted and cast it on the char buffer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Casting from struct to char array and vice versa
    By Edelweiss in forum C Programming
    Replies: 6
    Last Post: 08-08-2011, 10:35 PM
  2. Replies: 18
    Last Post: 05-06-2011, 07:22 PM
  3. Casting a 32-bit int as a char array
    By Invariant in forum C Programming
    Replies: 3
    Last Post: 01-28-2010, 06:14 AM
  4. Replies: 3
    Last Post: 03-31-2009, 12:34 PM
  5. Structs, members, and casting
    By Nick Howes in forum C Programming
    Replies: 4
    Last Post: 03-05-2008, 11:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21