Modifying a String Literal

Why there is no way out when starting this program.

#include<stdio.h> int main() { char* t="C++"; t[1]='p'; t[2]='p'; printf("%s",t); return 0; } 
+7
source share
4 answers

There are several other problems in the code.

  • Pointers are usually used to point to existing data, so you can use them like this:

    char arr [] = "C ++";

    char * t = & arr [0];

Also subject to change,

 t[1] = 'p'; t[2] = 'p'; 

Of course, there is a special way to use string - let the pointer point to the 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 to use it, which is more portable and understandable:

 const char* t="C++"; 

2. The code has a lot of space that does not conform to the 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 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 print line

Linux is buffered by line (ignore this if you use Windows: P), and for easier reading in the console you need to add "\ n" at the end of the printed line:

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

If you do not want to return the carriage after the line. The windows are used as you like:

printf("%s",t);

On Linux, you must add fflush () to stdlib.h.

 printf("%s",t); fflush(stdout); 
+8
source

"C ++" is a string literal stored in read-only and therefore cannot be changed. 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 

in fact -

 t[1] = 'p' ; 
+9
source

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.)

+6
source

Some versions of the standard runtime library require \n to trigger output.

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

All Articles