Re: Questions on conversions between char* to unsigned char* and vice versa
- From: Ben Bacarisse <ben.usenet@xxxxxxxxx>
- Date: Fri, 31 Dec 2010 17:20:04 +0000
Navaneeth <navaneethkn@xxxxxxxxx> writes:
I have few questions on conversions between "char*" to "unsigned
char*" and vice versa. I am assuming casting "unsigned char*" to
"char*" is safe because "char" can hold all the values that an
"unsigned char" can hold.
But conversion of "char*" to "unsigned char*" won't be safe as "char"
can hold more values. Is this understanding correct? On what cases
"char*" will have negative values?
There's been some confusion in the answers you've had. For one thing,
they reinforce your idea that the conversion of a char * to an unsigned
char * might be related to the range of values the char and unsigned
char can represent. This is not the case.
You can convert from a char * to an unsigned char * because the language
standard permits this.
Once you have done so, the characters pointed to are not converted when
you access them. Conversion has a special meaning in C, and it does not
apply here. Having done:
unsigned char *up = (unsigned char *)cp;
*up (or up) does not convert anything. It simple reinterprets the
first byte of whatever cp pointed to as an unsigned char -- i.e. as a
number from 0 to UCHAR_MAX (almost always 255).
I have never seen negative values on a "char*" string. So is that safe
to do conversion from "char*" to "unsigned char*"?
Yes, and it is safe regardless of whether there are negative char values.
You may view *any* object at all (and a string of chars is no different in
the respect) by converting a pointer to it to an unsigned char and
examining the bytes of the object by using that converted pointer.
By conversion, I mean using casting - char* c = (char*) string; where
string is a "unsigned char*".
This is also safe, but much less useful. char is an odd type -- it may
be signed or it may be unsigned so it is less useful that unsigned char
for examining objects. However, it safe to do this pointer conversion
and you'll do it often if you are working with unsigned char * and you
have to call library functions that expect a char * parameter.
Why I am using unsigned char
If any one wondering, why I use unsigned char - I use it for doing
some UTF8 processing on the string. I need to use that to skip the
multi-byte sequences correctly.
That's a perfectly valid reason to use unsigned char. You can do all
this using char * rather than unsigned char *, but I think the code is
clearer if you use unsigned char.
- Prev by Date: Re: standard doubt
- Next by Date: Re: Where to download C99 Standard
- Previous by thread: Re: Questions on conversions between char* to unsigned char* and vice versa
- Next by thread: Question about octal