473,396 Members | 2,093 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Casting between signed/unsigned pointer types.

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
2 3763
"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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Rade | last post by:
Following a discussion on another thread here... I have tried to understand what is actually standardized in C++ regarding the representing of integers (signed and unsigned) and their conversions....
24
by: laredotornado | last post by:
Hello, Are all pointer types the same length? My instinct tells me yes, but I just wanted to confirm with the experts. So if I have typedef struct { char* field1; int field2; }...
14
by: mr_semantics | last post by:
I have been reading about the practise of casting values to unsigned char while using the <ctype.h> functions. For example, c = toupper ((unsigned char) c); Now I understand that the standard...
2
by: DGR | last post by:
I need to have a 32-bit bit variable and I need to assign the variable values above the 32-bit signed limit (2,147,483,648). Can anyone help me understand what type I want? I tried UInt32 but...
5
by: max | last post by:
Dear all, I did the following analysis to conclude that the following pointer types are not compatible. Please let me know If my analysis and interpretation of the C standard are correct: ...
10
by: =?iso-8859-2?B?SmFuIFJpbmdvuQ==?= | last post by:
Hello everybody, this is my first post to a newsgroup at all. I would like to get some feedback on one proposal I am thinking about: --- begin of proposal --- Proposal to add...
3
by: tcsvikr | last post by:
unsigned int _I = _N; // _N is of type unsigned int for (_Grp = false; *_Pg != CHAR_MAX && '\0' < *_Pg && *_Pg < _I - _Np; _Grp = true)...
3
by: john | last post by:
As far as I know there is only the type wchar_t. However my compiler compiles both "signed wchar_t" and "unsigned wchar_t". Are there both signed and unsigned wchar_t types?
5
by: jason.cipriani | last post by:
There have been some recent threads about casting pointers to and from void* that have me rethinking some of my usual practices. I have a couple of questions. 1. What is the purpose of C++'s...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.