An important feature in C + + is pointers, which not only have the ability to obtain addresses, but also have the ability to manipulate addresses. Pointers can be used in arrays, or as parameters for functions, to access memory and operations on memory, and the use of pointers makes C + + efficient, but pointers are also very dangerous, and improper use can lead to more serious problems.
1, pointers
All the variables and constants in the program exist in a memory address, of course, the function also has a corresponding memory address, the different memory address can cause the program to perform differently.
Pointers are variables used to control and store memory addresses, which point to the address of a single object, except void, where the data type of the pointer is the same as the variable data type of the address to which it is directed.
2. How to define pointers, array pointers, function pointers
Common pointer definitions are 3 types: variable pointers, array pointers, and function pointers.
(1), the definition of the variable pointer
Copy Code code as follows:
int* p =; Define the pointer p and initialize the pointer to 0, which points to an address of 0000 0000
Or
int a=0; Define initialization constant A
int* p; Define Pointer P
p=&a; The pointer p points to the address of a, i.e. the pointer gets the address
(2), the definition of array pointers
Copy Code code as follows:
int a[]={0,1,2,3,4,5,6,7,8,9}; Defining arrays
int* P=a; Defines and assigns an array pointer, which gets the first address of the array
(3), the definition of function pointer
Copy Code code as follows:
int f (); Defining functions
Int (*p) (); Defining function pointers
P=f; An assignment function pointer that gets the first address of the function code
Distinguishes the definition of variable pointers, array pointers, and function pointers, as shown in the sample code.
#include <iostream> using namespace std;
int f ()//define a function {cout<< "Use of the test function pointer" <<endl<<endl;
return 0;
} void Main () {cout<< "========== variable pointer use ==========" <<endl;
int a=5; int* p =; Initialize the pointer to 0 int* q; Define Pointer q=&a; Assignment pointer cout<< "a =" <<a<<endl; Value of variable a cout<< "a =" <<*q<<endl; Value of variable a cout<< "p =" <<p<<endl; The address of the pointer p is 0000 0000 cout<< "&a =" <<&a<<endl; Get A's address cout<< "&a =" <<q<<endl;
Get the address of a cout<< "========== array pointer use ==========" <<endl;
int b[]={0,1,2,3,4,5,6,7,8,9}; int* pb=b; Address directly to the first element's address Cout<<pb<<endl//1th element, that is, the address of the first address of the array <<b<<endl//1th element, that is, the first address of the array <&l T;*pb<<endl//1th element's value << (*pb+2) <<endl;
The value of the 3rd element cout<< the use of the ========== function pointer ========== <<endl; int f (); define function int (*PF) (); //definition function pointer pf=f; An assignment function pointer that assigns the first address of the function to the pointer pf (*PF) (); Calling functions through function pointers}
The results are shown below:
3. Array pointer and array of pointers
An array pointer is a pointer variable that points to an array, where the pointer to an array is an array pointer, and the pointer array is an array containing only the pointer elements, and its elements can point to different objects of the same type.
4. function pointer and pointer function
A function pointer is a pointer to the address of a function's storage space, which can be assigned to a function pointer and called by a function pointer, which is essentially a pointer. The pointer function simply shows that it is a function that returns a pointer to a value, which is essentially a function.
5, reference delivery, value delivery and pointer delivery
In the C + + language, there are 3 ways in which the parameters and return values of a function are passed: value passing, reference passing, and pointer passing.
(1), Value transfer
A formal parameter is a copy of an argument, and changing the value of the parameter does not affect the value of the external argument. From the point of view of the invoked function, the value pass is a one-way (argument-> parameter), the value of the argument can only be passed in and cannot be transmitted. When the function needs to modify the parameters, and do not want this change to affect the caller, the use of value delivery.
(2), pointer delivery
A parameter is a pointer to an argument's address, which is equivalent to an operation on the argument itself when the parameter is pointed to.
(3), reference delivery
A formal parameter is equivalent to an "alias" of an argument. The operation of formal parameters is actually the operation of the actual parameters, in the process of reference transfer, the form parameter of the modulated function, although also as a local variable in the stack to open up the memory space, but at this time is stored by the main function to put in the argument variable address. Any operation of the called function on the formal parameter is treated as indirection, that is, the argument variable in the keynote function is accessed through the address stored in the stack. Because of this, any manipulation of the function on the formal parameter affects the argument variables in the keynote function.
Finally, summarize the similarities and differences between pointers and references :
Same point:
Is the concept of an address where the pointer points to a piece of memory whose content is the address of the memory, and a reference is an alias to a block of memory.
Different points:
The pointer is an entity and the reference is only an individual name;
References can only be initialized once at definition, then immutable, pointer variable, reference "one-woman", and pointers may be "inconstant";
The reference does not have a const, the pointer has a const,const pointer is immutable; (specifically, there is no int& const a form, and const int& A is, the former guidelines used in itself that alias can not change, of course, so do not need this form, The latter refers to the value of the reference can not be changed)
The reference cannot be null, the pointer can be empty;
The "sizeof reference" gets the size of the variable (object) to which it is pointed, and the "sizeof pointer" gets the size of the pointer itself;
The value of the pointer and the reference of the self increment (+ +) operation is different;
References are type-safe, and pointers are not (references are more type-checked than pointers).
The above is the entire content of this article, I hope to help you learn.