C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-26-2008, 09:40 PM   #1
UK2
 
Join Date: Sep 2003
Posts: 110
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
steve1_rm is offline   Reply With Quote
Old 03-26-2008, 09:51 PM   #2
Frequently Quite Prolix
 
dwks's Avatar
 
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;
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.
dwks is offline   Reply With Quote
Old 03-27-2008, 06:46 AM   #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   Reply With Quote
Old 03-28-2008, 02:25 PM   #4
Frequently Quite Prolix
 
dwks's Avatar
 
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   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 02:39 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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