Current location - Trademark Inquiry Complete Network - Futures platform - C language, why char str[]= "I love China" can be assigned, but Charstr [20]; Re-assignment is illegal?
C language, why char str[]= "I love China" can be assigned, but Charstr [20]; Re-assignment is illegal?
Let's start with char * str

Str= "I love China";

The essence of these two sentences is to open up a memory space in memory, put "I love China" into this memory space, and then give str the address of this memory space. Because str is a variable, it is legal to assign a value to it.

Look at char str[]= "I love China"; The function of this sentence is to define a char array, where str is the array name and the address of the first element. Since this is the definition of a variable, it is acceptable for the compiler to open the memory space, then put in the string and give the address to str.

Finally, look at charstr [20];

Str = "I love China!" ; In this sentence, firstly, a char array str is defined, which points to a memory space with a size of 20, and then a memory space is opened to put strings. At this time, the problem came. Str itself is a fixed value, so it is certainly unreasonable for you to give it a new address by force.