The string literal C creates an anonymous char array. Any attempt to modify this array has undefined behavior. Ideally, this could be accomplished by creating an array of const , but C did not always have const , and adding it to string literals would break the existing code.
char* t="C++";
This is legal, but potentially dangerous. An array containing the characters 'C', '+', '+', '\0' can 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 are trying to modify the contents of a string literal. The compiler should not warn you about this, either at compile time or at run time - and you donβt need to do anything to make it βworkβ.
If you want the compiler to know that the string is read-only, it is best to add a const qualifier:
const char *t = "C++";
Then the compiler should warn you if you try to change the string literal - at least if you try to do this through t .
If you want to change it, you must make t writable by array:
char t[] = "C++";
Instead of t pointing to a pointer to the beginning of "C++" , it makes a t array into which the contents of "C++" copied. You can do what you like with the contents of t if you do not go beyond it.
A few more comments on your code:
#include<conio.h>
<conio.h> specific to Windows (and MS-DOS). If you do not need your program to work on other systems, this is great. If you want it to be portable, delete it.
void main()
It is not right; correct declaration of int main(void) ( int main() doubtful in C, but it is correct in C ++.)
printf("%s",t);
Your output should end with a newline; various bad things can happen if it is not. Do it:
printf("%s\n", t);
And this:
getch();
refers to Windows. You probably need to close the output window after the program terminates, which is an unfortunate problem for Windows development systems. If you want a more standard way to do this, getchar() simply reads the character from standard input and allows you to press Enter to complete (although this does not give you a hint).
Finally, since main returns an int type result, it must do this; you can add
return 0;
before closing } . It really is not required, but it is not a bad idea. (C99 adds an implicit return 0; but Microsoft does not support C99.)