Why can a string be assigned to a char * pointer, but not to a char [] array?

Can someone explain why this works with a pointer:

char * str1; str1 = "Hello1"; str1 = "new string"; // but not this char str2 [] = "hello"; str2 = "four"; // or this char str3 []; str3 = "hello"; str3 = "hello"; 
+52
c string pointers initialization reusability
Jul 23 2018-11-21T00:
source share
5 answers

Why does this work with pointers:
When you say char * str1 in C, you are allocating a pointer in memory. When you write str1 = "Hello"; , you create a string literal in memory and point to it with a pointer. When you create another string literal "new string" and assign it to str1 , everything you do changes where the pointer points.

Why this does not work with arrays:
When you say char str2 [] = "Hello" , you create a string literal and put it in an array when it is defined. It is enough not to specify the size, since the array calculates it and adds '\0' . You cannot reassign anything to this array without changing its size. This is why str2 = "four" will not work.

In the case of str3 this is the same case. You did not determine the size of the array in the definition, so it calculated its size to 0. You cannot assign anything new without changing the size of the array.

+68
Jul 23 2018-11-11T00:
source share

Array and pointer are two different things, therefore. You can assign a pointer, but you cannot assign an array. A special exception is made to initialize char arrays with string literals.

 char a[] = "Hello"; //initialize a char array with string literal. Special case, OK char* p = "Hello"; //initializa a pointer with an array(which gets converted to pointer) p = "My"; //assign pointer to point to another value. OK a = "My"; //error, arrays cannot be assigned to. Use `strcpy` 

String literals (such as "Hello") are of type char[N] , where N is the number of characters (including the terminating '\0' ). An array can be converted to a pointer to its first element, but arrays and pointers are not the same thing as some bad books or teachers can say.

+19
Jul 23 2018-11-21T00:
source share

Simply put, because an array is not a first-class object in C / C ++. The only way to assign to an array is to use str (n) cpy or memcpy.

While the array is flushed to the pointer when passing the function, it is not possible to designate the array, except at the time of compilation as initialization.

+5
Jul 23 '11 at 21:30
source share

This is simply because when you write this code:

 char str2 [] = "hello"; 

or even:

 int arr[] = {1,2,4,4,5}; 

it creates str2 or arr as a constant pointer. Therefore, you cannot reassign any other values ​​to these pointers, and in a later case, you create a normal pointer, and you can assign something to it.

+3
Jul 22 '13 at 20:22
source share

The case with pointers This works because when you assign as str1="Hello" , you actually create a string literal called hello, allocating it somewhere in memory and assigning the first character of the literal to the pointer to the pointer, and since the pointer is not constant, you can assign it again with different addresses. Another important point is that the string literal is created in read-only memory.

The case with a character array You can assign a string literal to it, while initialization will be supported by the language. And do not confuse the assignment with initialization. While the destination, since its array of characters, you must change the character of the value by character, you are trying to address the first address of the string literal to the first character of the array (the name of the array returns the address of the first element of the array). And this is clearly wrong, since the first element is not a pointer, it cannot store the address.

+2
Feb 27 '17 at 5:22
source share



All Articles