Proper casting

This is a discussion on Proper casting within the C Programming forums, part of the General Programming Boards category; I want to cast a chunk of binary data to an array, how do I do that? I come from ...

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    14

    Proper casting

    I want to cast a chunk of binary data to an array, how do I do that? I come from a C++ background and haven't been doing this in a while. Its the array thats throwing me off. Casting as "uint8_t*" or "uint8_t[6]" give me wrong type and casting an array errors respectively.

    Code:
    void foo(char *rawdata) {
        uint8_t data[6];
    
        data = (???)rawdata;
    }

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Yeah... you can't cast that.

    You can do a strcpy() or a memcpy(), though.

    data is not a pointer. It's a fixed location in memory representing an array of data.

  3. #3
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    I thought data was a pointer to the first element to an array of 6 elements, so you can do this:

    Code:
    int i;
    unsigned int data[6];
    data[0] = 1; data[1] = 2; data[2] = 3; data[3] = 4; data[4] = 5; data[5] = 6;
    unsigned int *ptr = data;
    for(i = 0; i < 6; ++i)
    {
    	printf("&#37;u", *ptr++);
    }
    To solve your problem you should
    1. Know the size of the char array.
    2. copy the memory into the new array. (so data needs to be a pointer indeed!)
    Last edited by Ideswa; 06-27-2007 at 11:50 AM. Reason: sentence improval
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    It is a pointer in a sense, but you can't change what it points to. It points to the first element of the array and is const in that regard.

    You're just doing the opposite and having a pointer assigned to an array. The OP wants an array assigned to a pointer. Can't work that way.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,752
    If you want to know how to cast properly, ask a fisherman
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Quote Originally Posted by Salem View Post
    If you want to know how to cast properly, ask a fisherman
    Should be plenty of those around here, what with all the C's around.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  7. #7
    Registered User
    Join Date
    Jul 2006
    Posts
    14
    Thanks guys, that did the trick. Sorry for the simple question (and the more to come I'm sure). In the past I've just fumbled around with code til I got it to worked, but now I like actually getting an explanation. I appreciate it.

  8. #8
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    The name of an array decays to a pointer in a non-indexed context. But it's still not an l-value -- you can't assign to it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Casting
    By morvick in forum C++ Programming
    Replies: 2
    Last Post: 06-18-2007, 12:06 AM
  2. Casting Question (I think)
    By fayte in forum C Programming
    Replies: 6
    Last Post: 03-08-2006, 05:31 PM
  3. casting the system exstracted date into seperate ints
    By bazzano in forum C Programming
    Replies: 1
    Last Post: 08-30-2005, 01:17 AM
  4. Type casting
    By Lionmane in forum C Programming
    Replies: 28
    Last Post: 08-20-2005, 03:16 PM
  5. question about casting pointers/other types also??
    By newbie02 in forum C++ Programming
    Replies: 3
    Last Post: 08-07-2003, 06:01 AM

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