0
    int main()
    {
        char *ch="girl";
        int x=strlen(ch);
        *ch=ch[x];
        printf("%c",*ch);
        getch();
        return 0;
    }

Why there is a runtime error during the assignment of a NULL value to the pointer to character?

Patrick B.
  • 11,773
  • 8
  • 58
  • 101
  • 6
    It's undefined behaviour to modify a string literal. – P.P Feb 09 '13 at 12:53
  • 1
    If you are trying to print last character of string, you should change `*ch=ch[x]` to `*ch=ch[x-1]` – Tomáš Šíma Feb 09 '13 at 12:54
  • Consider to use `const` in such cases.. when declaring it as a `const`, you'll get a `compilation` error, which is **better** than a `runtime` error. – Maroun Feb 09 '13 at 13:16

3 Answers3

2

Replace

char *ch = "girl"

with

char ch[] = "girl"

Where the former creates a pointer to immutable memory, the latter creates a char[] array of the right size and initialises it with the letters of "girl" (including the terminating zero-byte).

Paul Hankin
  • 54,811
  • 11
  • 92
  • 118
  • So, if i simply declare as char *ch, will it be internally considered as a constant pointer to a character? – Anandhi Subramani Feb 09 '13 at 13:12
  • 1
    I think you're asking what `char *ch = "girl"` means. The pointer is of type `char *` so there's no compile-time error if you try to modify data through it. But at run-time, it's undefined behavior if you do this and the pointer isn't pointing at mutable memory. Just like it would be if `ch` was NULL or pointing at freed memory. – Paul Hankin Feb 09 '13 at 13:16
  • Can i get a still more specific answer of why creating a pointer makes the memory immutable and not the array? – Anandhi Subramani Feb 09 '13 at 13:28
1

UPDATE: thanks to @dreamlax

"girl" is implicitly declared as a char *. But most likely your compiler is putting the string-literals into a section (rostrings) which will later be placed in a protected memory-area. When you try to assign something to *ch it will access this protected (or not depending on your platform) memory.

The compiler should warn you about the char *ch = "girl";.

Patrick B.
  • 11,773
  • 8
  • 58
  • 101
  • 1
    `"girl"` is of type `char[5]` not `const char *`. – dreamlax Feb 09 '13 at 12:59
  • Then why iam getting a run time error since it is within the boundary? – Anandhi Subramani Feb 09 '13 at 13:01
  • Because your platform is putting the string literal in a protected/read-only memory. – Patrick B. Feb 09 '13 at 13:03
  • 2
    A variable of a const pointer type means that you can't modify the underlying memory through that pointer, and not that the underlying memory isn't modifiable. The problem here is that the memory associated with string literals is immutable (whatever the types of the variables). – Paul Hankin Feb 09 '13 at 13:03
  • @dreamlax you're right. I fixed my answer to what most likely causes the segfault in OP's case. – Patrick B. Feb 09 '13 at 13:06
  • What i have to do to modify the string by assigning any other string or character in this case? – Anandhi Subramani Feb 09 '13 at 13:06
  • @PatrickB What's an implicit declaration? Is it anything to do with a C declaration? – Paul Hankin Feb 09 '13 at 13:06
  • @Anonymous Answering this is off-topic to the question. Just so much, every literal in C (a number, char, char[], `NULL`) has an implicit type. – Patrick B. Feb 09 '13 at 13:08
  • So, if i simply declare as char *ch, will it be internally considered as a constant pointer to a character? – Anandhi Subramani Feb 09 '13 at 13:11
  • 1
    No, it's a char* pointer pointing at immutable memory. The type of the variable and whether the memory is immutable are not related. This is not a pleasant corner of the C language. – Paul Hankin Feb 09 '13 at 13:19
  • Can i get a still more specific answer of why creating a pointer makes the memory immutable and not the array? – Anandhi Subramani Feb 09 '13 at 13:22
  • @AnandhiSubramani: The string literal `"girl"` is stored as a 5-element array of `char` such that the array is allocated over the lifetime of the program. Depending on the implementation, this array may be stored in read-only memory. If you want to be able to modify the string, you have to create an array in your code and copy the *contents* of the string literal to it: `char myStr[] = "girl";`. You "own" the `myStr` array, so you may modify its contents; the string literal remains unaffected. – John Bode Feb 09 '13 at 14:28
-1

And this

int x=strlen(ch); //x=4
*ch=ch[x]; //you are out of bounds of array, because first element is 0, so last is 3
Michał Z.
  • 4,119
  • 1
  • 22
  • 32
  • 4
    There's no array, it's a string literal and `ch[strlen(ch)]` is not out of bounds: it references the zero-byte at the end. – Paul Hankin Feb 09 '13 at 12:56