One 、 What is a pointer ?
Definition : A pointer is the address of program data in memory , And pointer variables are variables that hold these addresses ;
The above a 4GB The memory of can store 2^32 Bytes of data . The consecutive hexadecimal number on the left is the memory address , Each memory address corresponds to a byte of memory space . The pointer variable holds this number , Memory address .
Pointer declaration :
A pointer is actually a variable , The way to declare a pointer is similar to a general variable declaration , as follows :
int *p; // Make a statement int Pointer to type p, The pointer points to a int Object of type
char *p // Make a statement char Pointer to type p, The pointer points to a int Object of type
int *arr[10] // Declare an array of pointers , The array has 10 Elements , Each of these elements is a point to int Pointer to type object
int (*arr)[10] // Declare an array pointer , The pointer points to a int One dimensional array of type
int **p; // Declare a pointer p , The pointer points to a int Pointer to type
Declaring a pointer variable does not automatically allocate any memory . Before making indirect access to pointers , The pointer must be initialized : Or point it to existing memory , Or dynamically allocate memory to him , Otherwise, we don't know where the pointer is , This problem needs special attention .
Two 、 What is a function pointer ?
Function pointer definition : A function pointer is a pointer variable to a function . therefore “ A function pointer ” First of all, it should be a pointer variable , It's just that the pointer variable points to the function . It's like pointing to an integer variable with a pointer variable 、 Character 、 Array is the same , Here is the pointing function .
Its general expression is : Type specifier (* Function name ) ( Parameters )
int (*fun)(int x) // Definition of function pointer
int (*fun)(int x,int y) // Definition of function pointer
Function pointer in PC Less used in software development , It is used more in the embedded industry , But whether it's PC Software or embedded software , Understand the definition and use of function pointers , It's good for understanding programming .
Assignment of function pointer
Function pointers, like other pointers, need to be initialized after they are defined and used .
Function pointers have two uses : Call function and do function parameters
int (*fun)(int x,int y) // Definition of function pointer
fun = &Function // How function pointers are assigned 1
fun = Function // How function pointers are assigned 2
x = (*fun)() // Call method of function pointer 1
x = fun() // Call method of function pointer 2
The address operator is taken when the function is assigned & Not required , Because a function identifier represents its address , In addition, the function does not need to be enclosed in parentheses ;
If it's a function call , It must also contain a parameter list enclosed in parentheses .
Function pointer usage
When we use the pointer , Need to go through the key * To get the value in the memory it points to , The same is true for function pointers . By using (*pf) Take out the function that exists in this address , Then call it. .
char* fun(char* p1,char* p2)
{
int i = 0;
i = strcmp(p1,p2);
if(0 == i)
{
return p1;
}
else
{
return p2;
}
}
int main()
{
char * (*pf)(char* p1,char* p2);
pf = &fun;
(*pf)("aa","bb");
return 0;
}
It's important to note here that yes , stay Visual C++6.0 in , When assigning a function pointer , It can be used &fun Or use the function name directly fun. This is because the function name is actually an address after being compiled , So there is no essential difference between the two usages .
The usage is extended to
When we are not satisfied with the simple use of function pointers , In this case, we need an advanced usage to extend our cognitive boundary of function pointer .
Interested students can take a look at the following usage , And try to understand how the expression is used by the function pointer .
(* (void(*)()) 0)(); // come from 《C Trap and Pitfalls》 This classic book
The answer is as follows : ``
First step : adopt void(*) (), You can see that this is a function pointer type . This function has no arguments , no return value .
The second step : adopt (void(*) ())0, You can see that this is going to 0 Cast to function pointer type ,0 It's an address , In other words, a function exists with the first address of 0 In a region of .
The third step : adopt (*(void(*) ())0), It can be understood that this is taking 0 Address the beginning of a section of memory content .
Step four : Finally understand (*(void(*) ())0)(), This is a function call .
Let the program jump to the absolute address of 0x0113F90C
Method 1 :
take 0x0113F90C Address cast to function pointer type , namely : (void (*)())0x0113F90C
And then call :((void (*)())0x0113F90C)()
Method 2 :
typedef (void (*)()) VoidFuncPtr;
((VoidFuncPtr)0x0113F90C)();
Interview questions : Point out program errors
#include<stdio.h>
void main(void)
{
int max(x,y);
int *p=max;
int a,b,c,d;
scanf("%d %d %d",a,b,c);
d=p(p(a,b),c);
printf("d:%d\n",d);
return;
}
int max(int a,int y)
{
return(x > y ?x:y);
}
answer :
int max(x ,y); Function declaration error , Change it to :int max(int x,int y);
analysis :max The function declaration just writes out the name of the formal parameter of the function , This is meaningless for the type of parameter , The compiler will put x and y As a data type , There will be errors when compiling ,max The call of must also be wrong .
int *p=max; Pointer definition error , Change it to :int (*p)(int ,int)=max;
analysis : The pointer to a function cannot be assigned directly to int Type pointer .
scanf("%d %d %d",a,b,c); Wrong use of library function , Change it to scanf("%d %d%d",&a,&b,&c);
analysis : Wrong use of library function , The second part should be the address of receiving data , It's written here as a variable .
d=p(p(a,b),c); Error calling function by function pointer , Change it to d=(*p)((*p)(a,b),c);`
analysis : The format of calling a function with a function pointer is as follows :(【*】【 Function pointer name 】)(【 parameter list 】); Can't call directly with function pointer and parameter .
3、 ... and 、 What is a pointer function ?
Pointer function definition : The foothold of pointer function is a function , The return value of this function is a pointer , And ordinary functions int function(int,int) similar , It's just that the returned data types are different .
_type_ *function(int, int) // The return is the pointer address
int function(int,int) // The return is int Type data .
int * fun(int x,int y) // Definition of pointer function
int* fun(int x,int y) // Definition of pointer function
All three of the above are correct , however * Closer to the return value is easier to understand .int *fun(int x) // Definition of pointer function
Pointer function call
When calling a pointer function , A pointer of the same type is required to receive the return value of its function .
typedef struct _Data{
int a;
int b;
}Data;
// Pointer function
Data* f(int a,int b){
Data * data = new Data;
data->a = a;
data->b = b;
return data;
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// Call pointer function
Data * myData = f(4,5);
qDebug() << "f(4,5) = " << myData->a << myData->b;
return a.exec();
}
However, you can also define its return value as void* type , When calling, cast the return value to the type you want .
The output is the same , However, this is not recommended , Because coercion can bring risks . The return type can be any basic type or compound type . A function that returns a pointer has a wide range of uses .
in fact , Every function , Even if it doesn't have a pointer that returns a certain type , It itself has an entry address , The address is equivalent to a pointer .
For example, a function returns an integer value , In fact, it is equivalent to returning the value of a pointer variable , But the variable is the function itself , And the whole function is equivalent to one “ Variable ”.
Four 、 Function pointer is different from pointer function
Through the above introduction , Partners should be able to understand the definition of both . So simply sum up the difference between the two :
1. Different definitions
A pointer function is essentially a function , Its return value is a pointer .
A function pointer is essentially a pointer , It points to a function .
2. The writing is different
Pointer function :int* fun(int x,int y);
A function pointer :int (*fun)(int x,int y);
It can be simply understood as , Pointer function * It's data type , The asterisk of the function pointer belongs to the function name .
More simply , You can distinguish the two in this way : The function name with parentheses is the function pointer , Otherwise, it's a pointer function .
3. Use different
The above function pointer and pointer function usage have , But function pointers will be used more , Relatively speaking, it is more difficult , For example, function pointers and callback functions , If it is C++ Non static member function pointer , There will be some differences in its usage , Interested students can follow up tweets or consult relevant books by themselves .
To make a long story short , It's easy to confuse these two things , We must have a deep understanding of the two definitions and differences , Avoid making mistakes .
See here , Are you right “C/C++” I have a little new understanding ~ If you like this article , Move your little finger , Pay attention to it ~
Programming learning books :
Programming learning video :