c - Wrong output from tolower fuction -
i have problem tolower function. tried use argv output $0@. what's wrong code?
#include <stdlib.h> #include <stdio.h> void makelower(char *s) { int i; for(i = 0; s[i] != '\0'; i++){ s[i] = tolower(s[i]); } printf("%s", s); } int main(int argc, char *argv[]) { argv[0]="a"; makelower(argv); return 0; }
argv
pointer pointer, char**
. function takes char*
. so, need pass like:
makelower(argv[0]);
but isn't going work because argv[0]
points string literal. modifying string literal undefined.
instead pass modifiable array like:
int main(int argc, char *argv[]) { char arr[] = "a"; makelower(arr); return 0; }
other option make copy of string literal passed (via argv[0]
) , able modify it. basically, idea can't legally modify string literals in c.
Comments
Post a Comment