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

CStringPointersInitializationReusability

C Problem Overview


Can someone explain why this works with the pointer:

char * str1;

str1 = "Hello1";

str1 = "new string";

// but not this
char str2 [] = "hello";
str2 = "four";

// or this
char str3 [];
str3 = "hello";
str3 = "hello";

C Solutions


Solution 1 - C

Why it works with pointers:
When you say char * str1 in C, you are allocating a pointer in the memory. When you write str1 = "Hello";, you are creating a string literal in memory and making the pointer point to it. When you create another string literal "new string" and assign it to str1, all you are doing is changing where the pointer points.

Why it doesn't work with arrays:
When you say char str2 [] = "Hello", you are creating a string literal and putting it in the array during its definition. It is ok to not give a size, as the array calculates it and appends a '\0' to it. You cannot reassign anything to that array without resizing it. That is why str2 = "four" will not work.

In case of str3, it is the same case. You haven't defined the size of the array in the definition, so it calculated its size to be 0. You cannot assign anything new without resizing the array.

Solution 2 - C

An array and a pointer are different things, that's why. You can assign to a pointer, but you can't assign to an array. A special exception is made for initialization of 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") have type char[N] where N is 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, whatever some bad books or teachers may say.

Solution 3 - C

Put simply, 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 an array collapses into a pointer when passed to a function, it is not possible to assign to an array, except at compile-time as initialisation.

Solution 4 - C

The case with pointers It works because when you are assigning like str1="Hello" , You are actually creating a string literal named hello allocating it somewhere in the memory , and assigning the address of first character of the literal to the pointer , and as the pointer is not constant you can assign it again with different addresses. And one more important point to note is that the string literal created are in read only memory.

The case with character array You can assign it a string literal while initialisation as that is supported by the language . And dont confuse assignment with initialisation. While assignment , since its an character array you have to change value character 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 return the address of first element of the array).And this clearly is not right as the first element is not pointer , it cant store address.

Solution 5 - C

It 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. That's why you can not reassign any other values to these pointers while in later case you are creating a normal pointer and you can assign anything to it.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnickView Question on Stackoverflow
Solution 1 - CtryurbestView Answer on Stackoverflow
Solution 2 - CArmen TsirunyanView Answer on Stackoverflow
Solution 3 - CJulianView Answer on Stackoverflow
Solution 4 - Cparth_07View Answer on Stackoverflow
Solution 5 - CSawanView Answer on Stackoverflow