Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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";
like image 932
nick Avatar asked Jul 23 '11 21:07

nick


People also ask

Can we assign string to char pointer?

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. String literals (such as "Hello") have type char[N] where N is number of characters (including the terminating '\0' ).

What is the difference between char * and char [] in C?

Difference between char s[] and char *s in CThe s[] is an array, but *s is a pointer. For an example, if two declarations are like char s[20], and char *s respectively, then by using sizeof() we will get 20, and 4. The first one will be 20 as it is showing that there are 20 bytes of data.

Why is a char * a string?

char *A is a character pointer. it's another way of initializing an array of characters, which is what a string is. char A, on the other hand, is a single char. it can't be more than one char.

What is the difference between char A ]= string and char * a string?

The main difference between them is that the first is an array and the other one is a pointer. The array owns its contents, which happen to be a copy of "Test" , while the pointer simply refers to the contents of the string (which in this case is immutable).


3 Answers

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.

like image 132
tryurbest Avatar answered Oct 19 '22 14:10

tryurbest


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.

like image 31
Armen Tsirunyan Avatar answered Oct 19 '22 12:10

Armen Tsirunyan


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.

like image 7
Julian Avatar answered Oct 19 '22 14:10

Julian