Casting from struct to char array and vice versa

This is a discussion on Casting from struct to char array and vice versa within the C Programming forums, part of the General Programming Boards category; Hi I want to ask about casting in C. If I have a struct c in which contains 2 integers ...

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

    Casting from struct to char array and vice versa

    Hi I want to ask about casting in C.

    If I have a struct c in which contains 2 integers a and b, and I want cast it to a char array, so I did this
    Code:
    typedef struct{
    char a;
    int b;
    }c;
    
    main(){
    char* ch;
    c c1;
    c* pter = (c*)malloc(sizeof(c));
    c* revert = (c*)malloc(sizeof(c));
    
    pter = &c1;
    pter->a = 5;
    pter->b=6;
    ch = (char)pter;
    
    revert = (c*)t;
    printf("priting revert c.a is %d",revert->a);
    printf("priting revert c.b is %d",revert->b);
    }
    When I tried to revert back to c struct and access the values of a and b I encounter some access reading violation reading location error.
    Any ideas what is wrong here? is this the way to do it?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Edelweiss View Post
    Hi I want to ask about casting in C.

    If I have a struct c in which contains 2 integers a and b,
    Code:
    typedef struct{
    char a;
    int b;
    }c;
    No you don't. You have an int and a char.
    Quote Originally Posted by Edelweiss View Post
    and I want cast it to a char array, so I did this
    Why?
    Quote Originally Posted by Edelweiss View Post
    Code:
    main(){
    char* ch;
    c c1;
    c* pter = (c*)malloc(sizeof(c));
    c* revert = (c*)malloc(sizeof(c));
    
    pter = &c1;
    pter->a = 5;
    pter->b=6;
    ch = (char)pter;
    
    revert = (c*)t;
    What is that supposed to be?
    Quote Originally Posted by Edelweiss View Post
    When I tried to revert back to c struct and access the values of a and b I encounter some access reading violation reading location error.
    Any ideas what is wrong here? is this the way to do it?
    Yeah, you are casting a pointer to a structure to be a character and assigning that to a character pointer. You also have a memory leak. Your compiler should be having a fit.


    Quzah.
    Last edited by quzah; 08-08-2011 at 10:17 PM.
    Hope is the first step on the road to disappointment.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,992
    I don't understand why you make it so complicated, with the use of malloc and all. I might demonstrate with something like:
    Code:
    #include <stdio.h>
    
    typedef struct {
        char a;
        int b;
    } c;
    
    int main(void) {
        c c1 = {'a', 123};
        char *ch;
        c *revert;
        ch = (char*)&c1;
        revert = (c*)ch;
    
        printf("priting revert c.a is %c\n", revert->a);
        printf("priting revert c.b is %d\n", revert->b);
        return 0;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    31
    oh my bad. It should be a struct with a char and int.

    I am confused on how casting between structs and arrays work. I want to find out how I can create a struct and cast it to a char array.

    this char array will then be sent to another function which will unpack it back to the struct, in which I just cast the struct back on the char array.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Hmm... I bet you could hide data in a structure's padding by casting back and forth (you could just use a union instead of casting, but I suppose if it's just for the sake of the experience...). I guess it should be legal to hide stuff in the padding. You would be assuming things about how your structure worked, but since unions would legally let you do this anyway, I suppose you should be able to.
    Code:
    struct foo
    {
        char c;
        long long l;
    };
    Let's say that after c we have padding to align l on the next word boundary. Casting to a character pointer (or sticking this structure in a union containing an array of characters) would let us plug whatever we want into the padding.

    At least it seems like you should be able to.


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

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,992
    Quote Originally Posted by Edelweiss
    this char array will then be sent to another function which will unpack it back to the struct, in which I just cast the struct back on the char array.
    Why do you have to go through this intermediate char array?

    Quote Originally Posted by quzah
    I bet you could hide data in a structure's padding by casting back and forth (you could just use a union instead of casting, but I suppose if it's just for the sake of the experience...). I guess it should be legal to hide stuff in the padding. You would be assuming things about how your structure worked, but since unions would legally let you do this anyway, I suppose you should be able to.
    Yes, I agree. Once you are dealing with the object as if it were an array of char, you can modify its bytes as you wish.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Aug 2011
    Posts
    31
    Quote Originally Posted by laserlight View Post
    Why do you have to go through this intermediate char array?
    Oh my initial thought is to create the char array is to create a char buffer to pass to a function that sends a byte stream through.
    But I have totally no idea on how to go about doing it. Hence creating the char array to try it out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. hash function - int to char string + vice versa
    By kerrymaid in forum C Programming
    Replies: 2
    Last Post: 05-29-2010, 11:01 AM
  2. Converting 32 bit binary IP to decimal IP (vice-versa)
    By Mankthetank19 in forum C Programming
    Replies: 15
    Last Post: 12-28-2009, 06:17 PM
  3. int to char and vice versa
    By mekaj in forum C++ Programming
    Replies: 13
    Last Post: 12-12-2005, 10:35 AM
  4. Problem: far to near copy, and vice versa
    By aaronc in forum C Programming
    Replies: 1
    Last Post: 06-16-2004, 06:37 AM
  5. Binary to ascii and vice versa
    By thenrkst in forum C++ Programming
    Replies: 13
    Last Post: 03-30-2003, 12:17 AM

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