This should be simple, but pointers to char arrays still leave me scratching my head sometimes. gcc complains that “subscripted value is neither array nor pointer” in lines 4 and 5, and “invalid type argument of ‘unary *'” on lines 8 and 11. Can someone explain what is going wrong here? I changed lines 4 and 5 to dereference the pointer first using brackets, but still can’t get what I want.
This should be a pretty simple function:
1 void makesafestr ( const char *unsafe, const char *safe )
2 {
3 int offset=0;
4 for (; (*safe)[offset] != "\0" ; offset++) {
5 switch ((*unsafe)[offset]) {
6 case "\n":
7 case "\r":
8 *safe[offset] = "~";
9 break;
10 default:
11 *safe[offset] = *unsafe[offset];
12 }
13 offset++;
14 }
}
In your code
safeis a pointer to a char (not a pointer to an array). So when you say*safethat’s a plainchar. Instead of(*safe)[offset]trysafe[offset]. Same goes for*safe[offset], same goes forunsafe.A second problem is the way you are comparing characters. “\0” is a string literal. In your code you want a character literal. In short, the
forshould be:But you could rewrite it and make it simpler, since
\0is0: