c - error convert character in string to uppercase -
i'm trying convert character of string uppercase letters
int main (void) { int = 0; int n = 0; static char *str[] = { "wow", "racecar", "no devil lived on.", "rotor" }; for(i = 0; < strlen(*str); i++) { if(str[i] != null) { n = function(str[i]); } } return 0; } int function(char* x) { int = 0; int j = strlen(x); char c; for(i = 0; < j; i++) { c = toupper(x[i]); x[i] = c; } return 0; }
i got error saying exc bad access, code 2
@ line x[i] = c;
i'm not sure why error, need create string , assign c new string? toupper return uppercase version of character didnt change element itself, i'm not sure wrong assigning value return toupper element.
your code attempts modify string literal , causes undefined behaviour.
the string "no devil lived on."
non-modifiable. compiler catch error should declare array as:
static char const *str[] = { "wow", // etc.
for historical reasons, compiler has let pass without breaking compilation if forget include const
. still error nonetheless, , compilers warn anyway.
for gcc can use flag -wwrite-strings
disable support historical case; cause error message generated code as-is.
Comments
Post a Comment