Modifying String Literal

A C string literal creates an anonymous array of char. Any attempt to modify that array has undefined behavior. Ideally this would be enforced by making the array const, but C didn't always have const, and adding it to string literals would have broken existing code.

char* t="C++";

This is legal but potentially risky. The array containing the characters 'C', '+', '+', '\0' could be stored either in read-write memory or in read-only memory, at the whim of the compiler.

t[1]='p';

Here your program's behavior is undefined, because you're attempting to modify the contents of a string literal. The compiler isn't required to warn you about this, either at compile time or at run time -- nor is it required to do anything to make it "work".

If you want to let the compiler know that the string is read-only, it's best to add the const qualifier yourself:

const char *t = "C++";

The compiler should then at least warn you if you attempt to modify the string literal -- at least if you attempt to do so through t.

If you want to be able to modify it, you should make t a writable array:

char t[] = "C++";

Rather than making t a pointer that points to the beginning of "C++", this makes t an array into which the contents of "C++" are copied. You can do what you like with the contents of t, as long as you don't go outside its bounds.

Some more comments on your code:

#include<conio.h>

<conio.h> is specific to Windows (and MS-DOS). If you don't need your program to work on any other systems, that's fine. If you want it to be portable, remove it.

void main()

This is wrong; the correct declaration is int main(void) (int main() is questionable in C, but it's correct in C++.)

printf("%s",t);

Your output should end with a newline; various bad things can happen if it doesn't. Make this:

printf("%s\n", t);

(The question originally included this line just before the closing } :

getch();

The OP later removed it. This is Windows-specific. It's probably necessary to keep your output window from closing when the program finishes, an unfortunate problem with Windows development systems. If you want a more standard way to do this, getchar() simply reads a character from standard input, and lets you hit Enter to finish (though it doesn't give you a prompt). Or, if you're running the program either from an IDE or from a command prompt, most of them won't close the window immediately.)

Finally, since main returns a result of type int, it should actually do so; you can add

return 0;

before the closing }. This isn't really required, but it's not a bad idea. (C99 adds an implicit return 0;, but Microsoft doesn't support C99.) (Update in 2019: Microsoft's support for C99 features is slightly better. I'm not sure whether the return 0; is necessary.)


"C++" is a string literal stored in read only location and hence cannot be modified. With this -

char* t="C++"; // t is pointing to a string literal stored in read only location

Instead, you should have -

char t[] = "C++" ;  // Copying the string literal to array t

to actually do -

t[1] = 'p' ;

There are several other problems with your code.

  1. Pointers are usually used to point to data that already exists, so you can use it like this

    char arr[] = "C++";

    char* t = &arr[0];

Also modifiable,

t[1] = 'p';

t[2] = 'p';

of course there is a special way of using string —— let the pointer point to a string constant. Just the way you used:

char *t = "C++";   // you cannot modify it in most operating systems
t[1] = 'p';
t[2] = 'p';

There is a better way of using it, which is more portable and easy to understand:

const char* t="C++"; 

2.You code have many place that is not in c standard

#include <stdio.h> // You'd better add a space between, for this is a good coding convention
#include <conio.h> // only supported by vc/vs in windows, you can use getchar() instead

int main()  // main returns int
{
    char* t = "C++";

    t[1] = 'p';
    t[2] = 'p';
    printf("%s\n", t);  // it's a good habit to add a '\n' when printing a string
    getchar();   // getchar() is supported by c standard library 

    return 0; // return 0 here
}

3.about printing string

Linux is line-buffered(ignore this if you are using windows :P) & for easier to read in console, you'd better add a '\n' at the end of you printed string:

printf("%s\n",t);

If you don't want to have a carriage return after a string. In windows use just as you like:

printf("%s",t);

In Linux, you should add a fflush() in stdlib.h.

printf("%s",t);
fflush(stdout);

Tags:

C