简体   繁体   中英

C strings - Do pointers to signed char vs unsigned char make a difference when working with strings?

This question originally springs from the fact that I'm currently working with OpenGL bindings from Rust.

Example signature of an OpenGL function which returns an unsigned char pointer:

const GLubyte* glGetString(GLenum name);

However, some functions in Rust which converts C strings to safe abstractions requires pointers of type const i8 * (so, equivalent to signed chars in C).

Now, my question is, does conversion between signed and unsigned char pointers make me potentially lose information about the string characters?

I know that both signed and unsigned characters have the same size, and I realize that converting between signed and unsigned types such as char changes the way that numbers are interpreted, but what I'm not sure about is whether this number interpretation actually matters for strings at the lower level, when printing them to the screen or saving them to files.

A conversion between pointer type never make you loses information because a pointer is just an address no matter in which type you store it.

A type of a pointer is just an information about how you would treat the pointed data.

For your issue, if you use only ASCII with 7 bits characters (for short, only the english characters) you will not loose any information by passing an unsigned to a signed char or a signed one to an unsigned. Because char type use generally 8 bits and the signed bits is the one not use by ASCII 7 bits.

A signed char or an unsigned char will have the same value as long as you stick with ASCII 7 bits.

Same as Stiven, except it doesn't matter even if it's UTF-8 8-bit characters. signed vs unsigned only produces different results when you do arithmetic operations on the items, and that should never happen for a string characters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM