Hello! ! !
char s[]="BOOK"
char s[10]="BOOK"
Both means that the array is initialized when it is declared, that is The definition is given first, that is, the memory space is allocated to the array, and the system puts the character "BOOK" into the space just now, so it is grammatical.
But
char s[10];
s[10]="BOOK"
and char s[10];
s[]="BOOK"
They are all wrong. The reason is: the syntax stipulates that if the character array is not initialized when it is declared, there will be no chance of direct assignment (specifically The reason will be discussed later)
To assign a value, you must use strcpy()
But it is different when we define a character pointer, for example:
char *p;
p="hello"; means that a pointer variable is defined at this time. At this time, the system will put the string="hello"; into the static storage area of ????the memory (that is, a type of memory, which is static The reason is that the value in cannot be modified) and assign the first address of the string to the pointer variable p, so that the pointer variable points to the string; this is okay.
Now let’s look at:
char s[10];
s[10]="BOOK"
and char s [10];
s[]="BOOK"
What is the reason:
char s[10]; first declares the character array, but No space is allocated, then
s[10]="BOOK" Because the array s has been declared, the system will first put the string into the static storage area, and then return a pointer, but at this time The left side of the equal sign is an array and it is as big as 10 bytes. Both sides of the equal sign are of different types. How can it be assigned a value?
The compiler will report an error when checking for errors.
Haha, I hope lz can understand.