Casting between signed/unsigned pointer types.

J

Jason Heyes

Is this the right idea?

char *ptr;
unsigned char *uptr = reinterpret_cast<unsigned char *>(ptr);

What about when I start with a char only?

char ch;
unsigned char *uptr = &static_cast<unsigned char>(ch);

Or should I use the reinterpret_cast again?

char ch;
unsigned char *uptr = reinterpret_cast<unsigned char *>(&ch);

I hope my question is clear. Thanks.
 
R

Ruslan Abdikeev

Jason Heyes said:
Is this the right idea?
char *ptr;
unsigned char *uptr = reinterpret_cast<unsigned char *>(ptr);
It will work.
What about when I start with a char only?
char ch;
unsigned char *uptr = &static_cast<unsigned char>(ch);
No way. static_cast returns an r-value here, so you cannot take an address.
Or should I use the reinterpret_cast again?
char ch;
unsigned char *uptr = reinterpret_cast<unsigned char *>(&ch);
This will work.

Hope it helps,
Ruslan Abdikeev.
 
J

Jason Heyes

Ruslan Abdikeev said:
It will work.

No way. static_cast returns an r-value here, so you cannot take an
address.

This will work.

Hope it helps,
Ruslan Abdikeev.

Ok I get it. Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top