By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,852 Members | 2,240 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,852 IT Pros & Developers. It's quick & easy.

Casting between signed/unsigned pointer types.

P: n/a
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.
Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:42**********************@news.optusnet.com.au ...
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.
Jul 23 '05 #2

P: n/a
"Ruslan Abdikeev" <ru*************@hotmail.com> wrote in message
news:37*************@individual.net...
"Jason Heyes" <ja********@optusnet.com.au> wrote in message
news:42**********************@news.optusnet.com.au ...
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.


Ok I get it. Thanks.
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.