Connecting Tech Pros Worldwide Forums | Help | Site Map

Casting between signed/unsigned pointer types.

Jason Heyes
Guest
 
Posts: n/a
#1: Jul 23 '05
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.



Ruslan Abdikeev
Guest
 
Posts: n/a
#2: Jul 23 '05

re: Casting between signed/unsigned pointer types.


"Jason Heyes" <jasonheyes@optusnet.com.au> wrote in message
news:42102c12$0$1023$afc38c87@news.optusnet.com.au ...[color=blue]
> Is this the right idea?
> char *ptr;
> unsigned char *uptr = reinterpret_cast<unsigned char *>(ptr);[/color]
It will work.
[color=blue]
> What about when I start with a char only?
> char ch;
> unsigned char *uptr = &static_cast<unsigned char>(ch);[/color]
No way. static_cast returns an r-value here, so you cannot take an address.
[color=blue]
> Or should I use the reinterpret_cast again?
> char ch;
> unsigned char *uptr = reinterpret_cast<unsigned char *>(&ch);[/color]
This will work.

Hope it helps,
Ruslan Abdikeev.


Jason Heyes
Guest
 
Posts: n/a
#3: Jul 23 '05

re: Casting between signed/unsigned pointer types.


"Ruslan Abdikeev" <ruslan_abdikeev@hotmail.com> wrote in message
news:37au11F5ah0a2U1@individual.net...[color=blue]
> "Jason Heyes" <jasonheyes@optusnet.com.au> wrote in message
> news:42102c12$0$1023$afc38c87@news.optusnet.com.au ...[color=green]
>> Is this the right idea?
>> char *ptr;
>> unsigned char *uptr = reinterpret_cast<unsigned char *>(ptr);[/color]
> It will work.
>[color=green]
>> What about when I start with a char only?
>> char ch;
>> unsigned char *uptr = &static_cast<unsigned char>(ch);[/color]
> No way. static_cast returns an r-value here, so you cannot take an
> address.
>[color=green]
>> Or should I use the reinterpret_cast again?
>> char ch;
>> unsigned char *uptr = reinterpret_cast<unsigned char *>(&ch);[/color]
> This will work.
>
> Hope it helps,
> Ruslan Abdikeev.[/color]

Ok I get it. Thanks.


Closed Thread


Similar C / C++ bytes