In-depth exploration of differences between C/C ++ array names and pointers
Author: Song Baohua E-mail: 21cnbao_at_21cn.com [at-> @]
1. Introduction
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. As a result, many
The program designer is confused. Many university teachers, in their C language teaching process, have to explain to the students: "array name is Pointer ".
Fortunately, my college teacher is one of them. Today, I am developing C/C ++ projects day after day, and it is still full
Programmers keep the misunderstanding that "array name is Pointer.
2. Magic array name
Please refer to the Program (this program is compiled on the Win32 platform ):
1. # include <iostream. h>
2. Int main (INT argc, char * argv [])
3 .{
4. Char STR [10];
5. char * pstr = STR;
6. cout <sizeof (STR) <Endl;
7. cout <sizeof (pstr) <Endl;
8. Return 0;
9 .}
2.1 array name is not a pointer
Let's first overturn "array name is 2.1 array name is not pointer
Let's overturn the argument that "array name is a pointer" and use reverse identification.
Verify that the array name is not a pointer
Assume that the array name is a pointer;
Then: Both pstr and STR are pointers;
This is because the pointer length is 4 on the Win32 platform;
Therefore, the output of rows 6th and 7th should be 4;
The actual situation is: 6th rows output 10, 7th rows output 4;
So: if not, the array name is not a pointer.
2.2 array name-like pointer
We have proved that the array name is indeed not a pointer, but let's look at the 5th rows of the program. This row of programs directly assign the array name to the pointer, which seems that the array name is indeed a pointer!
We can also find examples of array names that look like pointers:
1. # include <string. h>
2. # include <iostream. h>
3. Int main (INT argc, char * argv [])
4 .{
5. Char str1 [10] = "I love u ";
6. Char str2 [10];
7. strcpy (str2, str1 );
8. cout <"String Array 1:" <str1 <Endl;
9. cout <"String Array 2:" <str2 <Endl;
10. Return 0;
11 .}
The two parameters that can be accepted in the original form of the strcpy function in the Standard C library are char pointers, but what we pass to it in the call is two array names!
Function output:
String Array 1: I love u
String Array 2: I love u
The array name looks like a pointer again!
Since the array name is not a pointer, why use the array name as a pointer everywhere? As a result, many programmers come to the conclusion that the array name (primary) is)
It is not a pointer (BIN ).
The entire Devil.
3. array name Disclosure
So it is time to expose the essence of the array name. First, we will give three conclusions:
(1) The meaning of array name is that it refers to an object as a data structure, which is an array;
(2) the extension of an array name is that it can be converted into a pointer pointing to its object, and it is a pointer constant;
(3) the pointer to the array is another variable type (in Win32 platform, the length is 4), which only means the address of storing the array!
3.1 array name represents a Data Structure: Array
Now we can explain why the output of 1st rows of 6th programs is 10. According to conclusion 1, the meaning of the array name STR is a data structure, that is
Char array with a length of 10, so sizeof (STR) results in the memory size occupied by this data structure: 10 bytes.
Let's look at it again:
1. Int intarray [10];
2. cout <sizeof (intarray );
The output result of rows 2nd is 40 (The memory size occupied by integer arrays ).
If the C/C ++ program can be written as follows:
1. Int [10] intarray;
2. cout <sizeof (intarray );
We all understand that intarray is defined as an instance of the int [10] data structure. Unfortunately, C/C ++ currently does not support this definition method.
3.2 array names can be used as pointer Constants
According to conclusion 2, the array name can be converted to a pointer pointing to the object. Therefore, the array name of row 5th in program 1 is directly assigned to the pointer, and the program 2 7th
The row directly uses the array name as the pointer parameter.
Is the following program established?
1. Int intarray [10];
2. intarray ++;
You can compile it to discover compilation errors. The reason is that although the array name can be converted to a pointer pointing to the object, it can only be regarded as a pointer constant and cannot be modified.
Pointers, whether directed to struct, array, or basic data types, do not contain the meaning of the original data structure. On the Win32 platform, sizeof
The operation result is 4.
By the way, we can correct another misunderstanding of many programmers. Many Programmers think that sizeof is a function. In fact, it is an operator,
It looks like a function. The statement sizeof (INT) indicates that sizeof is indeed not a function, because the function accepts the form parameter.
(A variable), no C/C ++ function in the world accepts a data type (such as INT) as a "form parameter ".
3.3 Data names may lose their data structure Connotation
It seems that the magic problem of the array name has been successfully solved, but the calm lake has once again set off a wave. See the following program:
1. # include <iostream. h>
2. Void arraytest (char STR [])
3 .{
4. cout <sizeof (STR) <Endl;
5 .}
6. Int main (INT argc, char * argv [])
7 .{
8. Char str1 [10] = "I love u ";
9. arraytest (str1 );
10. Return 0;
11 .}
The output result of the program is 4. Impossible?
4. A terrible number, as mentioned above, is the length of the pointer!
Conclusion 1: the meaning of Data names is the data structure of arrays. In the arraytest function, STR is the array name. Why is the result of sizeof?
Is the pointer length? This is because:
(1) When the array name is used as a function parameter, In the function body, it loses its own meaning and is just a pointer;
(2) Unfortunately, it also loses its constant feature while losing its connotation. It can perform auto-increment, auto-subtraction, and other operations and can be modified.
Therefore, when the data name is used as a function parameter, it is completely reduced to a common pointer! Its aristocratic identity is denied, and it becomes the only possession of 4
Bytes.
The above is conclusion 4.