Pointers in C + +, array pointers and arrays of pointers, function pointers and pointer functions
This article from the beginner's point of view, the understanding of what is a pointer, how to use pointers, how to define pointers, how to define array pointers and function pointers, and give corresponding examples of the demonstration; then, the array pointer and pointer array, function pointer and pointer function are distinguished, and finally, the most commonly confused reference is passed, Value passing and pointer passing are done at the area.
One of the most important features in C + + is the pointer, which has the ability to obtain an address, as well as an operation address. Pointers can be used in arrays, or as parameters of functions, to access memory and to manipulate memory, and pointers are used to make C + + efficient, but pointers are also dangerous, and improper use can lead to more serious problems.
1. Pointers
All variables and constants in the program exist in a memory address, and of course, the function has a corresponding memory address, the different memory address will cause the program to be executed differently.
Pointers are variables used to control and store memory addresses, which point to the address of a single object, and the data type of the pointer is consistent with the variable data type of the address, except void.
2. How to define pointers, array pointers, function pointers
Common pointers are defined in 3 ways: variable pointers, array pointers, and function pointers.
(1), the definition of variable pointers
int* p=0;//defines the pointer p and initializes the pointer to 0, that is, the address that points to 0000 0000 or int a=0;//defines the initialization constant aint* p;//defines the pointer pp=&a;//pointer p to the address of a, that is, the pointer gets the address
(2), the definition of array pointers
int a[]={0,1,2,3,4,5,6,7,8,9};//defines an array int* p=a;//defines and assigns an array pointer, which is the first address of the array
(3), the definition of function pointers
int f ();//define function int (*p) ();//define function pointer p=f;//assignment function pointer, i.e. get first address of function code
The sample code that distinguishes the definition of variable pointers, array pointers, and function pointers is as follows.
#include <iostream>using namespace Std;int f ()//define a function {cout<< "use of test function pointers" <<endl<<endl; return 0;} void Main () {cout<< "========== variable pointer use ==========" <<endl;int a=5;int* p=0;//initialize pointer 0int* q;// ;//Assignment Pointer cout<< "a =" <<a<<endl;//the value of variable a cout<< "a =" <<*q<<endl;//the value of variable a cout<< The address of "p =" <<p<<endl;//pointer p is 0000 0000cout<< "&a =" <<&a<<endl;//Gets the address of a cout< < "&a =" <<q<<endl;//Gets the address of a cout<< "========== array pointer use ==========" <<endl;int b[]={ 0,1,2,3,4,5,6,7,8,9};int* pb=b;//directly points to the address of the first element cout<<pb<<endl//the address of the 1th element, the first address of the array <<b<<endl/ /1th element address, that is, the value of the first address of the array <<*pb<<endl//the 1th element << (*pb+2) <<endl;//The value of the 3rd element cout<< "========== function pointers are used ========== "<<endl;int f ();//define function int (*PF) (),//define function pointer pf=f;//assignment function pointer, assign the first address of the function to the pointer pf (*PF) ();// Calling a function through a function pointer}
Results such as:
3. Array pointers and arrays of pointers
An array pointer is a pointer variable that points to an array, which is an array pointer to an array, and the pointer array is an array that contains 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 a function's storage space address, which can be assigned to a function pointer and called by a function pointer, which is essentially a pointer. The pointer function simply means that it is a function whose return value is a pointer, which is essentially a function.
5. Reference passing, value passing, and pointer passing
In the C + + language, the parameters and return values of a function are passed in 3 ways: value passing, reference passing, and pointer passing.
(1), Value transfer
The parameter is a copy of the argument, and changing the value of the parameter does not affect the value of the external argument. From the point of view of the called function, a value pass is one-way (argument-and-parameter), and the value of the argument can only be passed in and not outgoing. The value is passed when the parameter needs to be modified inside the function and you do not want the change to affect the caller.
(2), pointer passing
A parameter is a pointer to the address of an argument that, when directed to a parameter, is the equivalent of an action on the argument itself.
(3), reference delivery
The formal parameter is equivalent to the "alias" of the argument, the operation of the formal parameter is actually the operation of the argument, in the reference transfer process, the parameters of the function is called as a local variable in the stack to open up the memory space, but this is stored by the key function is put in the address of the argument variable. Any operation of the modulated function on the formal parameter is handled as an indirect addressing, that is, the argument variables in the central Melody function are accessed through the address stored in the stack. Because of this, any action taken by the modulated function on the parameter affects the argument variables in the keynote function.
Finally, summarize the same points and different points of pointers and references:
Same point:
Is the concept of the address, the pointer to a piece of memory, its content is the address of the referred memory, and the reference is the alias of 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 the time of definition, immutable, pointers variable, reference "mindedness", pointers "inconstant";
The reference does not have a const, the pointer has const,const pointer is immutable, (refers to does not have int& const a This form, but the const int& A is some, the former guideline uses itself namely the alias cannot change, this is of course, therefore does 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) pointed to, and the "sizeof pointer" gets the size of the pointer itself;
Pointer and reference self-increment (+ +) operation has different meanings;
The reference is type-safe, and the pointer is not (the reference is more type-checked than the pointer).
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Pointers in C + +, array pointers and arrays of pointers, function pointers and pointer functions