simple question about pointers and casting

This is a discussion on simple question about pointers and casting within the C++ Programming forums, part of the General Programming Boards category; Hello, I am just experimenting with some code. And I am not sure why I need to cast to an ...

  1. #1
    UK2
    Join Date
    Sep 2003
    Posts
    112

    simple question about pointers and casting

    Hello,

    I am just experimenting with some code. And I am not sure why I need to cast to an unsigned char*.

    I get this error: cannot convert from 'unsigned char' to 'unsigned char *'
    I can understand that the pointer is an unsigned char. But if I am assigning the address of the myFloat which I am casting a unsigned char. Why do I need to cast it as a pointer as well?

    Code:
    float myFloat = 10000;
    unsigned char *ptrChar;
    ptrChar = (unsigned char *) &myFloat;
    
    //Why can't I just do this?
    ptrChar = (unsigned char) &myFloat;
    The following example using a int:
    Code:
    int myInt = 1000;
    int *ptrInt = &myInt;
    As the pointer is an int and I am assigning the address of myInt this works perfectly ok.

    However, with the example above I have converted to a unsigned char.

    Many thanks for clearing this up.

    Steve

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    It's because "unsigned char" is a different type from "unsigned char *". Observe.
    Code:
    char c;
    char *p = &c;
    unsigned char uc = (unsigned char)c;
    unsigned char *up = (unsigned char *)p;
    An unsigned char is (usually) one byte. It usually stores a value from 0 to 255.
    A unsigned char * is usually four or eight bytes. It stores the address of another variable, the address of an unsigned char somewhere else in memory.

    So unsigned char and unsigned char * are completely different types. You have to tell the compiler which of them you want to use. Both can be used, but both give you very different values.
    Code:
    unsigned char c = 'a';
    unsigned char *anotherp = (unsigned char *)c;
    unsigned char anotherc = (unsigned char)c;
    The value of anotherp is dubious, but it can be done if you really want to.

    Rationale: when specifying types, use the name of the type (like "unsigned char"), as well as any levels of indirection (with asterisks).

    Incidentally, casting a float * to an unsigned char * isn't usually much use unless you are trying to examine each byte of the float individually or something.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Also, if you're using C++ you shouldn't use C style casts. C++ has 4 safer casts:
    static_cast
    dynamic_cast
    const_cast
    reinterpret_cast

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    In this case you'd probably use static_cast<>. Assuming you're using C++, of course.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

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, 09:23 PM
  2. Replies: 8
    Last Post: 01-23-2008, 04:22 AM
  3. casting pointers
    By zedoo in forum C Programming
    Replies: 6
    Last Post: 10-09-2005, 05:15 AM
  4. casting int and float pointers
    By mhaak in forum C Programming
    Replies: 3
    Last Post: 05-13-2005, 06:19 PM
  5. Casting function pointers into char arrays and back
    By coolman0stress in forum C++ Programming
    Replies: 2
    Last Post: 07-05-2003, 02:50 PM

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