Correct. Strings provided as string-literals are read-only. You will need to copy the string to mutable memory first:
char* s_readonly = "foobar";
size_t s_length = strlen( s_readonly );
char* s_mutable = malloc( s_length + 1 );
assert( s_mutable );
errno_t err = strcopy_s( s_mutable, s_length, s_readonly );
assert( err == 0 );
to_lower_case( s_mutable ); 
puts( s_mutable );
free( s_mutable );
- My code performs an explicit copy to the heap. This code could be made simpler by assigning the string literal to a char[n]-type. This has the advantage of allowing staticsizeof()to work which is fasterO(1)thanstrlenwhich isO(n).
- printf("%s\n", s)can be replaced with- putswhich is faster as there's no format-string to parse.
- I use strcopy_sover the insecurestrcopy. Always check the returned error code!
- And always call freeaftermalloc. If you're dealing with short strings you could useallocainstead which is faster and doesn't require the use offree.
Alternatively, just this:
char s_mutable[] = "foobar";
to_lower_case( s_mutable ); 
puts( s_mutable );
The advantage here (besides terseness) is that s_mutable is mutable right away, and it means that sizeof( s_mutable ) == 12.