And differences in the use of C language function pointer to a function pointer, the function pointer, the structure of

A pointer function

definition

Function pointer, simple terms, is a function of the return pointer, which is essentially a function, the function return value is a pointer.
Statement format is: * type identifier  function name (parameter list)

This does not seem difficult to understand, describe further.
Consider the following function declaration:

int fun(int x,int y);

This function should be very familiar, in fact, a function, then the return value is an int type, is a value.
Then look at the following function declaration:

int *fun(int x,int y);

This function only difference is that the above function name in front of more than a number, * and this function is a pointer to the function. The return value is an int type pointer is an address.

Such description should be easy to understand, so-called pointer function is also nothing special, but the contrast is a normal function and returns a pointer (ie the address value) only.

Writing pointer function

int *fun(int x,int y);
int * fun(int x,int y);
int* fun(int x,int y);

The wording of personal habits, in fact, if * is close to the return type, then it may be easier to understand its definition.

Examples

(The syntax is the same, just different output due to the development of Qt I used to, so here for convenience, the example is written in Qt project)
look at a very simple example:

typedef struct _Data{
    int a;
    int b;
}Data;

//指针函数
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);
    //调用指针函数
    Data * myData = f(4,5);
    qDebug() << "f(4,5) = " << myData->a << myData->b;

    return a.exec();
}

Output is as follows:

f(4,5) =  4 5

Note: When calling a function pointer, a need to receive the same type of return value of a pointer function thereof.
But it can also be defined as the return value of type void *, when calling cast the return type is what you want, as follows:

//指针函数
void* f(int a,int b){
    Data * data = new Data;
    data->a = a;
    data->b = b;
    return data;
}

调用:
Data * myData = static_cast<Data*>(f(4,5));

The output result is the same, but such use is not recommended, because the cast could pose a risk.

Second, the function pointer

definition

Function pointer, which is essentially a pointer, the pointer to the function. In summary, the function pointer is a pointer to a function.
Statement format: type specifier (* function name) (parameter)
as follows:

int (*fun)(int x,int y);

Function pointer is the need to address a function assigned to it, there are two writing:

fun = &Function;
fun = Function;

& Address operator is not required, because it means that the identifier is a function of its address, if the function call, must also contain a parenthesized parameter table.

Calling the function pointer there are two ways:

x = (*fun)();
x = fun();

Available in two ways, in which the second looks normal function call lacks distinction, if possible, it is recommended to use the first, because you can clearly indicating that this is a pointer to call a function by the way. Of course, it also depends on personal habits, if you understand its definition, just how to use all the line.

Examples

int add(int x,int y){
    return x+y;
}
int sub(int x,int y){
    return x-y;
}
//函数指针
int (*fun)(int x,int y);

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    //第一种写法
    fun = add;
    qDebug() << "(*fun)(1,2) = " << (*fun)(1,2) ;
	//第二种写法
    fun = &sub;
    qDebug() << "(*fun)(5,3) = " << (*fun)(5,3)  << fun(5,3);

    return a.exec();
}

Output is as follows:

(*fun)(1,2) =  3
(*fun)(5,2) =  2 2

The above mentioned several assignments and call me all the way were used, and its output is the same.

Difference between the two

Through the above description, it should be able to clearly define its understanding of both. So simply summarize difference between the two:

Different definitions

Pointer to the function is essentially a function that returns the pointer value.
Essentially function pointer is a pointer that points to a function.

Different wording

Pointer to the function: int * fun (int x, int y);
function pointers: int (* fun) (int x, int y); can brute understood that the pointer to the function * belongs to the data type, the function pointer the asterisk is part of the function name.
Then simply, this can distinguish the two: function name in parentheses is a function pointer, function pointer otherwise.

Different uses

As already written a detailed example, not here in a long-winded.

All in all, it is easy to confuse two things, both of which must be in-depth understanding of the definition and distinction to avoid mistakes.

In addition, it is introduced for the ordinary function pointer, if it is non-static C ++ member function pointers, its use will have some differences, in another blog post presented separately, article https://blog.csdn.net/luoyayun361 / article / details / 101109522

 

Function pointers three structural body

Defined function pointer

General function pointer can be so defined:

int(*func)(int,int);

Int denotes a pointer containing two parameters and the return value is a function pointer int any form if there is a function:

    int add(int x,int y)
    {
        return x+y;
    }

It can be achieved in actual use pointer func:

func=&add; // pointer assignment, or func = add; add & same meaning and add

printf("func(3,4)=%d\n",func(3,4));

Structure function pointer contains

In fact, in the structure, it may be the same as a general variable that contains the function pointer variable. Here is a simple implementation.

#include <stdio.h>  
struct TEST  
{  
	int x,y;  
	int (*func)(int,int); //函数指针  
};  
  
int add1(int x,int y)  
{  
	return x*y;  
}  
  
int add2(int x,int y)  
{  
	return x+y;  
}  
  
void main()  
{  
	struct TEST test;  
	test.func=add2; //结构体函数指针赋值  
	//test.func=&add2; //结构体函数指针赋值  
	printf("func(3,4)=%d\n",test.func(3,4));  
	test.func=add1;  
	printf("func(3,4)=%d\n",test.func(3,4));  
}  
  
/* 
输出: 
func(3,4)=7 
func(3,4)=12 
*/  

C language, how to realize the function function in the structure? The body is made of a similar structure and type, so that his internal attributes, the method also has
such a structure is generally called a protocol type, reference:

#include <stdio.h>  
  
typedef struct  
{  
int a;  
void (*pshow)(int);  
}TMP;  
  
void func(TMP *tmp)  
{  
    if(tmp->a >10)//如果a>10,则执行回调函数。  
    {  
        (tmp->pshow)(tmp->a);  
    }  
}  
  
void show(int a)  
{  
    printf("a的值是%d\n",a);  
}  
  
void main()  
{  
    TMP test;  
    test.a = 11;  
    test.pshow = show;  
    func(&test);  
}  

Structure function pointer assignment

Usually used as follows to assign the function pointer structure, this embodiment is used in the linux kernel:

struct test                                        
{
	int (*add) (int a,int b);
	int (*sub) (int a,int b);
	int (*mult) (int a,int b);
};

int  test_add(int a,int b)  
{
   return (a+b);
}
int  test_sub(int a,int b) 
{
   return (a-b);
}
int  test_mult(int a,int b) 
{
   return (a*b);
}

struct test testp={  
	 .add  = test_add, 
	 .sub  = test_sub, 
	 .mult = test_mult,
 };

 

Published 19 original articles · won praise 4 · Views 2091

Guess you like

Origin blog.csdn.net/a419116194/article/details/104065134