String operations with unsigned char arrays

B

Boon

Hello everyone,

I've read Jack's page several times.
http://home.att.net/~jackklein/c/inttypes.html#char

I'm puzzling over...

"""
Q: What other differences are there between the three types of char?

A: All three types of char are different types. A pointer to one type of
char cannot be assigned to a pointer to another char type without a
cast. This is also true when passing a pointer as an argument to a function.
"""

Does this mean I can't (i.e. I'm not supposed to) use the standard
string functions such as strcpy, strcmp with unsigned char arrays?

How does one write string-manipulation code that is portable between
platforms where the signedness of plain char differs?

Do I need to write my own string library?

e.g.

$ cat chars.c
#include <string.h>
int main(void)
{
char s1[256];
unsigned char s2[256];
signed char s3[256];

strcpy(s1, "toto");
strcpy(s2, "tutu");
strcpy(s3, "titi");
strcpy(s1, s2);
strcpy(s2, s3);
strcpy(s3, s1);
return 0;
}

$ gcc -O2 -std=c89 -pedantic -Wall -Wextra chars.c
chars.c: In function 'main':
chars.c:9: warning: pointer targets in passing argument 1 of 'strcpy'
differ in signedness
chars.c:10: warning: pointer targets in passing argument 1 of 'strcpy'
differ in signedness
chars.c:11: warning: pointer targets in passing argument 2 of 'strcpy'
differ in signedness
chars.c:12: warning: pointer targets in passing argument 1 of 'strcpy'
differ in signedness
chars.c:12: warning: pointer targets in passing argument 2 of 'strcpy'
differ in signedness
chars.c:13: warning: pointer targets in passing argument 1 of 'strcpy'
differ in signedness

Regards.
 
F

Fred

Hello everyone,

I've read Jack's page several times.http://home.att.net/~jackklein/c/inttypes.html#char

I'm puzzling over...

"""
Q: What other differences are there between the three types of char?

A: All three types of char are different types. A pointer to one type of
char cannot be assigned to a pointer to another char type without a
cast. This is also true when passing a pointer as an argument to a function.
"""

Does this mean I can't (i.e. I'm not supposed to) use the standard
string functions such as strcpy, strcmp with unsigned char arrays?

How does one write string-manipulation code that is portable between
platforms where the signedness of plain char differs?

Do I need to write my own string library?

No. It means exactly what it says. Just use a cast when the explicit
type name of one of the variables differs from the explicit type
name of the other.

For variables typed "char", you do not need a cast, regardless
of whether char is implemented as signed or unsigned.
 
E

Eric Sosman

Boon said:
Hello everyone,

I've read Jack's page several times.
http://home.att.net/~jackklein/c/inttypes.html#char

I'm puzzling over...

"""
Q: What other differences are there between the three types of char?

A: All three types of char are different types. A pointer to one type of
char cannot be assigned to a pointer to another char type without a
cast. This is also true when passing a pointer as an argument to a
function.
"""

Does this mean I can't (i.e. I'm not supposed to) use the standard
string functions such as strcpy, strcmp with unsigned char arrays?

Yes.
How does one write string-manipulation code that is portable between
platforms where the signedness of plain char differs?

By treating char as "plain char" and not assuming things
about its sign. When the sign becomes important (as in your
"Incorrect index computation" thread), use casts as needed.
Do I need to write my own string library?

Probably not. What are you trying to do to/with these
arrays of unsigned char (which are not "strings" as C uses
the term)?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top