Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Question about casting and pointers

Status
Not open for further replies.

robUK2

Programmer
Mar 10, 2008
57
TH
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top