char * abc = "ABC";
int i;
printf("%s\n", abc);
for (i = 0; i < strlen(abc); i++)
{
abc[i] = tolower((int) abc[i]); //Error at this line
}
printf("%s\n", abc);
The line where I call tolower does not execute?
char * abc = "ABC";
int i;
printf("%s\n", abc);
for (i = 0; i < strlen(abc); i++)
{
abc[i] = tolower((int) abc[i]); //Error at this line
}
printf("%s\n", abc);
The line where I call tolower does not execute?
char * abc = "ABC";
defines a string literal
which cannot be changed during runtime.
Use char abc[] = "ABC";
edit: Maybe you "can" change it in some cases,
but there is no guarantee for anything.
It could work, it could crash,
or doing other strange things with your program
which are not immediately recognizable.
The abc points to a constant string, and because it is read-only, you can not change the value directly. There are 2 methods.
(1)You can allocate the memory in the stack:
char abc[] = "ABC";
int i;
printf("%s\n", abc);
for (i = 0; i < strlen(abc); i++)
{
abc[i] = tolower((int) abc[i]); //Error at this line
}
printf("%s\n", abc);
(2)You can also allocate the memory in heap, and the code is like this:
int len = strlen("ABC");
char *p = malloc(len + 1);
strcpy(p, "ABC");
printf("%s\n", p);
for (i = 0; i < len; i++)
{
p[i] = tolower((int) p[i]); //Error at this line
}
printf("%s\n", p);