2

I am trying to modify string "hello" to "Hello" but it's not working,neither it gives any error .Can someone please explain why it's not working.

#include <stdio.h>

int main() {
    char *arr[] = {"hello" , "world"};
    char **p = arr;
    printf("%s\n",arr[0]);
    *(*(p+0)+1) = 'H';
    printf("%s\n",arr[0]);
    return 0;
}
David Kiger
  • 1,958
  • 1
  • 16
  • 25

3 Answers3

6

string literals are of type char[] and are stored in read only memory. You cannot alter them.

If you want to alter them you need to create a char array. What you have a is an array of pointers.

You can do this:

char foo[] = "Hello";
foo[0] = 'G';
printf("%s", foo);
Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
  • String literals are of type `const char[len + 1]`, where `len` is the length of the string. –  Mar 10 '13 at 14:01
  • 1
    String literals are not const in C. – netcoder Mar 10 '13 at 14:04
  • 2
    They're type `char []`, not `const` - but if a program modifies the literal, it's UB. – teppic Mar 10 '13 at 14:04
  • It's not universally true that string literals are stored in read-only memory; that depends on the architecture. That's why the behavior is left *undefined*. – John Bode Mar 10 '13 at 15:17
3

Because you can't modify string literals (despite them being of an array type of non-const char). Your program invokes undefined behavior as-is.

  • no, string literals in C unfortunately are `char[]`. In C++ they are `char const[]`. – Jens Gustedt Mar 10 '13 at 14:04
  • @JensGustedt I've been told several times that they're `const char[]` and also that they're `char []`. Both in the context of C. If I recall correctly, the reasoning for `char []` was that it's a backward-compatibility feature with K&R C and that it's true in C89. I don't know whether this has changed in C99, could you please cite the standard? I hate that every time I post an answer about this issue, somebody from the opposite group comes here and talks me off. –  Mar 10 '13 at 14:07
  • @H2CO3: 6.4.5.5 - "For character string literals, the array elements have type `char`"; + .6: "If the program attempts to modify such an array, the behavior is undefined." (I just happened to be checking this recently) – teppic Mar 10 '13 at 14:09
  • C11 still says: *For character string literals, the array elements have type char, and are initialized with the individual bytes of the multibyte character sequence.* and then *If the program attempts to modify such an array, the behavior is undefined.* So yes they are `char[]` and no, you don't have the right to modify them. – Jens Gustedt Mar 10 '13 at 14:10
0
char *arr[] = {"hello" , "world"};  

arr is array of pointers to char, so you cannot modify the string literals, the pointers are pointing to.

char arr[][6] = {"hello" , "world"}; 

arr is 2 dimensional array of chars - or array of two strings. You can modify them, but you need to specify the length of the second dimension of the array. It needs to be big enough to hold the longest strings.

Nuclear
  • 1,316
  • 1
  • 11
  • 17