I started to learn C language in my sophomore year. After a few years, I have been catching up with new technologies. The underlying things are really not thoroughly understood. I am free today. After reading a lot of information, I feel that I must write it down, if you forget it, you can check it later.
Let's talk about arrays and pointers:
(1) Distinguish between statements and definitions in C Language
The declaration only tells the type and name of the compiler variable. The definition determines the memory allocation. In a C program, there can be only one definition, but the Declaration can have multiple types.
For example, the Code in file 1: int A [10]; defines an array. the compiler will allocate 10 integer memories to array.
For example, the Code in file 2: extern int A []; declares an external array pointing to the memory variable A allocated by the compiler in file 1, and does not allocate memory in the Declaration, therefore, you do not need to specify the array size.
For example, the Code in file 3: extern int A []; Also declares an external array. It also points to the variable A allocated by the compiler in file 1, and does not allocate memory in the Declaration, therefore, you do not need to specify the array size.
(2) left and right values
Such as a = B; such a simple statement contains many nuances, which also reflects the essence of pointers in C language. Pointer value (address) and pointer pointing (content in the address)
Here, a appears on the left of the equal sign. It is the left value, which indicates the address represented by A. It is determined during compilation. The left value indicates the place where the result is stored.
Here, B appears on the right of the equal sign, which indicates the content of the address corresponding to B. the right value can be known only when the program is running.
C language has a very important feature: "modifiable left value"
For example, int A = 10;
Int B = 100;
A = B;
The code above can be correctly run, but this feature is indeed not suitable for arrays. We know that the array name is equivalent to an address value and a left value, but it cannot be used as a value assignment object.
For example, int A [10];
Int B [10];
A = B; // error. A is not the left value that can be modified.
We normally understand that the values on the left of the equal sign can be assigned values, but the array is an exception. Therefore, a special term such as "modifiable left value" is generated in C language.
(3) differences between char * S1 = "abcdefg" and char S2 [] = "abcdefg"
Before analyzing the differences, it is necessary to analyze the memory distribution of the c program first,
On the left is the memory distribution description, with a piece of code in the middle. The arrow points to the memory area, and on the right is the value of the variables monitored during debugging in vs2010, here we will focus on the differences between char S1 [] = "abcdefg" and char * S2 = "abcdefg:
S2 is a pointer pointing to the string's first address. S1 is a character array, and its value also points to the string's first address. Although both strings are: "abcdefg", the compiler essentially differs in the storage space they are allocated from the results of left and right side monitoring, the S1 value is 0x0012ff40 and the S1 address value is 0x0012ff40. This indicates that S1 is allocated to the stack space and the corresponding string is also in the stack space.
Then observe that the value of the pointer variable S2 is 0x00000073c, and the address value of S2 is 0x0012ff34. This indicates that the symbol S2 is allocated to the stack space, however, the content "abcdefg" is in the text constant area.
This essential difference leads to the differences between pointer and array access methods, as shown in the following code:
Char S1 [] = "abcdefg"; char c = S1 [1]; the compiler symbol table S1 has a fixed address value 0x0012ff40. during runtime, because the offset 1 is used together with 0x0012ff40, obtains the content of the added address value.
Char * S2 = "abcdefg"; char c = S2 [1]; the compiler symbol table S2 has a fixed address 0x0012ff34. When running, the content in 0x0012ff34 is 0x00000073c, add offset 1 and 0x00000073c,
Obtain the content of the added address.
Compare the two methods above. The pointer has one more step.
The string corresponding to the pointer cannot be modified, but the array can
That is to say, the following code is incorrect:
Char * S2 = "abcdef ";
S2 [1] = 'K ';
The following error occurs during running:
The array can be modified, such as the Code:
Char S1 [] = "abcdef ";
S1 [1] = 'K ';
The root cause of this difference is the memory allocation above. One is in the stack area and the other is in the text constant area.