![]() |
| | #1 |
| UK2 Join Date: Sep 2003
Posts: 110
| simple question about pointers and casting 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; Code: int myInt = 1000; int *ptrInt = &myInt; However, with the example above I have converted to a unsigned char. Many thanks for clearing this up. Steve |
| steve1_rm is offline | |
| | #2 |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,698
| 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; 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; 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. |
| dwks is offline | |
| | #3 |
| and the hat of sweating Join Date: Aug 2007 Location: Toronto, ON
Posts: 3,285
| 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 |
| cpjust is offline | |
| | #4 |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,698
| 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. |
| dwks is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Casting and Pointers | kpreston | C Programming | 10 | 08-28-2008 08:23 PM |
| Question about casting (relating to qsort and pointers to structures) | kuda | C Programming | 8 | 01-23-2008 04:22 AM |
| casting pointers | zedoo | C Programming | 6 | 10-09-2005 04:15 AM |
| casting int and float pointers | mhaak | C Programming | 3 | 05-13-2005 05:19 PM |
| Casting function pointers into char arrays and back | coolman0stress | C++ Programming | 2 | 07-05-2003 01:50 PM |