C Pointer to bind strings to array notation

Why does the first version cause the program to crash and the second not? Aren't they the same?

Pointer designation

char *shift = "mondo"; shift[3] = shift[2]; 

Array designation

 char shift[] = {'m', 'o', 'n', 'd', 'o', '\0'}; shift[3] = shift[2]; 

MWE

 int main( void ) { char *shift = "mondo"; shift[3] = shift[2]; char shift[] = {'m', 'o', 'n', 'd', 'o', '\0'}; shift[3] = shift[2]; return 0; } 
+6
source share
5 answers

No! This is one of the important issues in C. First, you create a pointer to a read-only part in memory, i.e. You cannot change it, just read it. Secondly, an array of characters is created, i.e. Part of the memory of continuous characters, where you can have both read and write access, which means that you can read and change the values โ€‹โ€‹of the array.

+6
source

The first points to a string literal (usually in a read-only section of the code, there really needs to be a const char * , but can go away with it due to historical reasons) |.

The second creates an array, and then fills that array.

Therefore they are not the same

+3
source

The first is memory allocation in the .TEXT segment, and the second is in .BSS. The memory in the .TEXT segment is, in fact, read-only or const :

  char *string = "AAAA"; 

This creates what const char * is effectively, as the memory will be allocated in the .TEXT segment as a string literal. Since this will usually be read-only, attempting to write to it will result in access violation or segmentation failure.

You want to do this:

  char string[] = "AAAA"; 

This will work as expected and allocate memory for a string of four As capital elements and use the string variable as a pointer to a location.

+2
source

This creates a pointer to an existing line:

 char *shift = "mondo"; 

This creates a new array of characters:

 char shift[] = {'m', 'o', 'n', 'd', 'o', '\0'}; 

In the second case, you are allowed to change the characters, because these are the ones you just created.

In the first case, you simply point to an existing line that should never be changed. The details of where the string is stored depends on the specific compiler. For example, it can store a string in immutable memory. The compiler is also allowed to do tricks to save space. For instance:

  char *s1 = "hello there"; char *s2 = "there"; 

s2 can actually point to the same letter 't', which is located at the seventh position of the line pointed to by s1 .

To avoid confusion, prefer to use constant pointers with string literals:

 const char *shift = "mondo"; 

Thus, the compiler will tell you if you accidentally try to change it.

+1
source

Whenever you define a string using char * str = "hello"; This is implicitly expressed by the const char * str= "hello"; This means that this symbol goes into read mode only.

But in the case of an array, the same is interpreted as char const *array[]; This is why the compiler screams when the user tries to change the base address of the array. This is implicitly done by the compiler.

+1
source

All Articles