C/C ++ should be the first programming language in the university, and their importance is self-evident. However, I have always held the incorrect creed "array name is Pointer" until now, probably because the incorrect explanation from a college teacher keeps me confused. Pointers are characteristic of the C/C ++ language, while array names are too similar to pointers. In many cases, array names can be used as pointers, therefore, it is inevitable to confuse the two in learning. Here we will introduce the differences between pointers and array names:
1. The size of the memory occupied by the pointer and array name is different, as shown in program 1 below:
- Char str [10];
- Char * pstr = str;
- Cout & lt; <sizeof (str)> & lt; <endl;>
- Cout & lt; <sizeof (pstr);>
- </Sizeof (pstr);>
- The output result of the first row is 10, and the output result of the second row is 4 </endl;> </sizeof (str)>
From this we can see that the array name corresponds to (instead of pointing to) a piece of memory (memory area occupied by the array) or refers to the data structure of the array, its address and capacity remain unchanged during the life cycle, and only the content of the array can be changed. The pointer corresponds to a memory area that occupies 4 bytes (Win32) and points to the memory unit corresponding to the address stored by the 4 bytes. It can point to any type of memory block. Therefore, the sizeof (str) value is the memory space occupied by the array, that is, 10 bytes, while the sizeof (pstr) value is the memory space occupied by the pointer value, that is, 4 bytes.
2. the array name is not a pointer, but under certain circumstances it is converted to a pointer that refers to the first address of the array, and the pointer converted from this array name can only be a pointer constant.
You can perform this conversion only in the following two cases:
A. In the second line of code of program 1, assign the array name directly to the pointer. At this time, the array name is converted to the constant pointer pointing to the first unit of the array.
B. When the array name is directly used as a pointer parameter, the array name is converted to a constant pointer pointing to the first unit of the array for transmission, as shown in program 2 below:
- Void fun (char str [])
- {
- Cout & lt; <sizeof (str)> & lt; <endl;>
- Str ++;
- }
- Void main ()
- {
- ...
- Char str1 [5];
- Fun (str1 );
- ...
- }
- </Endl;>
- Note: When the array name is passed as a function parameter, it is no longer a pointer constant in the Child function, but a real pointer, which can be increased or decreased, can be modified. Therefore, the value of sizeof (str) output by the first statement in program 2 is 4. </sizeof (str)>
Since the array name can be regarded as a pointer constant and the constant cannot be modified, the following code is not allowed:
- Char str [10];
- Str ++;
The following code is valid:
- Char str [10];
- Char * pstr = str;
- Pstr ++;
3. Accessing Arrays Using pointers is essentially different from accessing arrays using array names.
For example:
- Char str [7] = "ksarea ";
- Char * pstr = str;
- Cout & lt; <str [3]> & lt; <endl> & lt;
- <Pstr [3];>
- </Pstr [3];> </endl>
- Both str [3] and pstr [3] Return 'R' characters, but the Execution Code generated by the compiler is different. For str [3], the Execution Code starts from str, moves two bytes backward, and then extracts the characters. For pstr [3], the Execution Code extracts the address from pstr, add 3 to it, and then retrieve the characters in the corresponding memory. Of course, if pstr is a pointer to the int type, the processing process of pstr [3] is to retrieve the address from pstr and add 3 * sizeof (int) to it ), finally, take out the characters in the corresponding memory, and the other data types, and so on. <Span id = "more-73"> </span> <! -- More --> <! -- More --> <! -- More --> <! -- More --> <! -- More --> <! -- More --> <! -- More --> </str [3]>