Re: Questions on conversions between char* to unsigned char* and vice versa



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[0]) 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.

--
Ben.
.



Relevant Pages

  • Re: Exercise 5-9 K&R
    ... Thus a + 1 points to the second char in the array, ... before conversion and char * after conversion. ... mov eax, [puts] ... puts, after conversion, has type pointer to function, so *puts has ...
    (comp.lang.c)
  • Re: malloc() and implicit cast
    ... implicitly converted to char* without any warning at all. ... "A pointer to an object or incomplete type may be ... So the conversion from int* to char* is allowed. ...
    (comp.lang.c)
  • Re: pipe question
    ... really an anonymous char array) is converted to a pointer to its first ... This pointer is ... void * and char * are not compatible types, ... conversion between them, making them "assignable" and, since argument ...
    (comp.unix.programmer)
  • Re: Strange problem on GCC - PLEASE HELP!!
    ... be a pointer to char, and you are passing pointer to unsigned char. ... Do not pass pointers to character arrays that are not strings to ...
    (comp.lang.c)
  • Putty key format.
    ... /* Hacked up ssh-add.c code to convert an OpenSSH key into a format ... static char *comment = NULL; ... int length; ... static unsigned char * buffer = NULL; ...
    (comp.security.ssh)