Why can a string be assigned to a char* pointer, but not to a char[] array?
In C++, string literals, such as "hello", are essentially character arrays that end with a null character () to mark the end of the string. This string literal has a fixed address in memory, which can be referenced using a pointer.Using PointersWhen we assign a string literal to a pointer, we are essentially storing the memory address of the string in the pointer. For example:Here, "hello" is a constant string stored in the program's read-only data segment. merely holds the address of this data, so this assignment is valid.Using ArraysHowever, when we attempt to assign a string literal to a character array, the situation is different. For example:In this case, the content of the string literal "hello" is copied into the array. This is done during compile-time initialization, and the array actually holds a copy of "hello". After this, as an array has its own memory space and can be modified.However, if we attempt to assign a string to the array after declaration, as follows:This is not allowed. Because the array name is a constant pointer, its value (i.e., the starting address of the array) cannot be changed. Once the array is declared, you cannot make it point to another address; you can only operate or modify the elements within the array using indices.SummaryWhen using pointers, you can point the pointer to different string literals or character arrays at any time.When using arrays, you can only initialize string literals at declaration time, and afterward you cannot change the address it points to.This difference is primarily determined by C++'s type safety and memory management rules.