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.
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.