1. array name is not a pointer
- 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 .}
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.
Note: pointers, whether directed to struct, array, or basic data types, do not contain the meaning of the original data structure. On the WIN32 platform, the sizeof operation results are 4.
Many Programmers think that sizeof is a function. In fact, it isOne OperatorBut 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 "shape parameter ".
2. the array name is like 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 .}
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!
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 (main) is (that) Not a pointer (object ).
Secret array name
Now it is time to reveal the essence of the array name. Let's draw three conclusions:
(1) The meaning of array name is that it refers to an object as a data structure, which is an array;
(2)Array nameIts Extension lies in itsIt can be converted to a pointer pointing to its object.AndIsPointerConstant(Cosnt pointer, pointer cannot be changed .);
(3)Pointer to arrayIt is another variable type (in WIN32 platform, the length is 4), which only means the storage address of the array!
Note:"Constant pointerAndPointer constant"
"Constant pointer"(Pointer to the const object) refers to the amount of data on the address.
"Pointer constant":( Cosnt pointer) the address is a constant, and the data on the address can be changed.
3. Data names may lose their data structure
- 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.
Conclusion 1: The data name is represented by an array. In the arrayTest function, str is the array name. Why is the result of sizeof 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) Sorry,While losing its meaning, it also loses its constant feature. It can perform auto-increment, auto-subtraction, and other operations and can be modified.
So,When the data name is used as a function parameter, it is completely reduced to a common pointer.! Its aristocratic identity is deprived of being a 4-byte civilian.