0

I've found the following code in a legacy project that seems to cause some trouble:

for( ; *str; ++str )
    *str = tolower(*str);

The trouble is, we get an SIGSEGV at the moment the result of tolower() should be written back to *str . This is what I got from using printf() debugging and the stracktrace produces, as we're using JNI here and thus attaching a debugger to the C libraries is not working for us.

The code fails if it's compiled with gcc on linux. Running the same thing on windows (CMake is being used for this cross-plattform setup) works fine.

Where can I look to find the reason for this? Any hints appreciated :)

thank you - Markus

Markus
  • 610
  • 1
  • 9
  • 23

1 Answers1

2

String literals are non-modifiable:

char blop[] = "modifiable string";
char *bla = "non-modifiable string";

*blop = tolower(*blop); // OK
*bla = tolower(*bla);   // not OK, modifying a string literal
ouah
  • 142,963
  • 15
  • 272
  • 331
  • +1, but `tolower()` function doesn't expect `char*` and doesn't modify anything ;-) What you meant is what the OP refers to as `myfunction()` in comments above. – Michael Krelin - hacker Apr 10 '12 at 09:30