Thread: casting pointers

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    16

    casting pointers

    hi,

    i have:

    Code:
    struct test{
           int a;
           int b;
           char c[20];
    };
    
    struct test *ptest;
    is it now safe to do something like this:

    Code:
    BYTE *b = (BYTE *) ptest;
    int tmp;
    for (tmp = 0; tmp < sizeof(struct test); tmp++){
       BYTE c = *(b+tmp)
       do_something(c)
    }

    why would i want to do this? i have a function that needs to treat with arbitrary data, something like this:

    Code:
    send_data(BYTE *data, int length);
    and i want to pass that struct into that function.


    felix

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > i have a function that needs to treat with arbitrary data, something like this:
    Most people make such functions accept a void* parameter, then the casting problem goes away.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    16
    unfortunatly i cannot change that functions signature because it comes from a library...

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    "Safe" depends on your definition. Yes, you're allowed to access the individual bytes in a struct using the mechanism you're using. In fact, if you look up fread() and fwrite() you'll find it allows you to output data in this way (if you use appropriate casts). What is less safe is any interpretation of the meaning of those bytes: the layout of data in structures is often operating system and compiler dependent.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    16
    Thanks Grumpy.

    lets say now that i copyy those bytes each one after another into a freshly allocated BYTE Buffer and cast the thing back into a struct test pointer, e.g. like this:

    Code:
    BYTE * b = malloc(sizeof(struct test) * sizeof(BYTE));
    .... copy every single byte
    struct test *reborn = (struct test *) b;
    is this valid?

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Yes it is valid, within some constraints.

    The thing that is not necessarily valid is;
    Code:
        struct test *a = malloc(sizeof(struct test));
        /*   initialsie members of a */
        fwrite(a, sizeof (struct test), 1, some_file_stream);
    and (with code built using another compiler)
    Code:
        struct test *a = malloc(sizeof(struct test));
        fread(a, sizeof (struct test), 1, a_stream_for_the_same_file);
    
         /* access members of a */
    These two bits of code will interoperate correctly if built using the same compiler (and same compiler settings) on the same operating system. They will not necessarily interoperate if the code is built using different compiler (or different compiler settings) or on a different operating system. The reasons are that;

    1) sizeof (struct test) is implementation dependent;
    2) layout of individual members in a struct is implementation dependent.

  7. #7
    Registered User
    Join Date
    Sep 2005
    Posts
    16
    good, thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Casting and Pointers
    By kpreston in forum C Programming
    Replies: 10
    Last Post: 08-28-2008, 08:23 PM
  2. simple question about pointers and casting
    By steve1_rm in forum C++ Programming
    Replies: 3
    Last Post: 03-28-2008, 02:25 PM
  3. Replies: 8
    Last Post: 01-23-2008, 04:22 AM
  4. casting to void pointers dynamically
    By dp_goose in forum C++ Programming
    Replies: 3
    Last Post: 07-19-2003, 12:46 AM
  5. Casting function pointers into char arrays and back
    By coolman0stress in forum C++ Programming
    Replies: 2
    Last Post: 07-05-2003, 01:50 PM