0
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?

jpw
  • 44,361
  • 6
  • 66
  • 86
  • 2
    Compile-time or runtime error? How does it manifest, error message, unexpected behavior? –  Mar 03 '14 at 01:13
  • 2
    Please be more specific what you mean by "does not execute". – Raymond Chen Mar 03 '14 at 01:13
  • Did you include ``? – jpw Mar 03 '14 at 01:14
  • When I try to run, my program freezes at at point. I know because I added a print statement before that and it worked. But then the program crashed after that. – user3263416 Mar 03 '14 at 01:15
  • 1
    "ABC" is a static string. They cannot be modified (as far as the standard goes). Don't do this and do `strcpy(abc, "ABC")` instead. – Guido Mar 03 '14 at 01:16
  • Yes, I did include – user3263416 Mar 03 '14 at 01:16
  • 1
    You're trying to modify a literal string in-place... you can't do that. `abc` should be `const`. – TypeIA Mar 03 '14 at 01:17
  • Use `char abc[] = "ABC";` so you can modify the memory. Do not use `strlen(abc)` in the condition of a loop like that — calculate the length once before the loop and test against that. It doesn't matter much when the string is only 3 characters long; it does if you start working with multi-kilobyte strings as it converts a linear O(N) algorithm into a quadratic O(N^2) algorithm — which is bad! – Jonathan Leffler Mar 03 '14 at 01:46
  • possible duplicate of [Why do I get a segmentation fault when writing to a string?](http://stackoverflow.com/questions/164194/why-do-i-get-a-segmentation-fault-when-writing-to-a-string) – Dennis Meng Mar 03 '14 at 02:04

2 Answers2

5

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.

deviantfan
  • 11,268
  • 3
  • 32
  • 49
  • I remember the bad old days when this was not true; we did some "interesting" things with literals. – Lawrence Dol Mar 03 '14 at 01:18
  • What "bad" means depends on the reader...playing with interesting dirty code stuff is fun :D But not when you actually want the program to work reliable :D – deviantfan Mar 03 '14 at 01:22
2

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);
Nan Xiao
  • 16,671
  • 18
  • 103
  • 164