C++ pointer functions and function pointers

Except for functions of void type, functions must have a return value after the call ends, and the pointer can also be the return value of the function. When the return value of a function is a pointer type, the function is a pointer function. The main purpose of using a pointer function is to return a large amount of data from the called function to the calling function at the end of the function. Usually, after the non-pointer function call ends, only one variable or object can be returned.

1. Pointer function

Except for functions of void type, functions must have a return value after the call ends, and the pointer can also be the return value of the function. When the return value of a function is a pointer type, the function is a pointer function. The main purpose of using a pointer function is to return a large amount of data from the called function to the calling function at the end of the function. Usually, after the non-pointer function call ends, only one variable or object can be returned.

The general definition form of a pointer function is:

数据类型 *函数名(参数表)
{
    
    
	函数体
}

The data type indicates the type of pointer returned by the function; the function name and "*" identify a pointer function; the parameter table is the formal parameter list of the function.

【example】

int* fun(int size)
{
    
    
	int* arr = (int*)malloc(size*sizeof(int));
	return arr;
}

int main()
{
    
    

	int* p = fun(5);
	if (*p != NULL)
	{
    
    
		for (int i = 0; i < 5; i++)
		{
    
    
			p[i] = i + 1;
			cout << p[i] <<"  ";
		}
		cout << endl;
	}
	free(p);

	return 0;
}

operation result:

insert image description here

2. Function pointer

When the program is running, not only the data occupies the memory space, but also the code that executes the program is transferred into the memory space and occupies a certain amount of space. **Each function has a function name. In fact, this function name represents the starting address of the function code in memory. ** From this point of view, the essence of the usual form of calling a function "function name (parameter table)" is "the first address of the function code (parameter table)".

The function pointer is a variable specially used to store the first address of the function code. In the program, you can use the pointer to the function to call the function like using the function name. That is to say, once a function pointer points to a function, it has the same effect as the function name. The function name not only indicates the starting address of the function code, but also includes information such as the return value type of the function and the number, type, and arrangement order of the parameters. Therefore, when a function is called by the function name, the compilation system can automatically check whether the actual participation formal parameters are consistent, and when the return value of the function is used to participate in other operations, it can automatically perform a type consistency check.

(1) Declare a function pointer

When declaring a function pointer, you also need to specify the return value and formal parameter list of the function. The general syntax is as follows:

数据类型 (*函数指针名)(形参表)

The data type indicates the return value type of the function pointed to by the function pointer; the content in the first parentheses indicates the name of a function pointer; the formal parameter table lists the type and number of formal parameters of the function pointed to by the pointer.

[Note] Since the definition of function pointers is complex in form, if there are multiple such definitions in the program, repeating such definitions many times will be quite cumbersome. A good solution is typedef .

For example:

typedef int(*fun)(double);

This declares fun to be an alias of type "pointer to function with one parameter of double and return type of int". Below, when you need to declare a variable of this type, you can use it directly:

fun funprt;

This declares a function pointer with the name funptr of that type. Complex types can be easily aliased with typedefs.

(2) Use of function pointers
Function pointers must be assigned before they are used, and the pointers point to the starting address of an existing function code. The general syntax is:

函数指针名=函数名;

The function name on the right side of the equal sign must indicate a function that has been declared and has the same return type and the same formal parameter list as the function pointer. After the assignment, the function pointed to by the pointer can be directly referenced through the function pointer name.

[Example] function pointer instance

void A(float)
{
    
    
	cout << "这是一个A函数" << endl;
}

void B(float b)
{
    
    
	cout << "b的值为:" << b << endl;
}

void C(float c)
{
    
    
	cout << "c的值为:" << c << endl;
}

const float PI = 3.14159f;
const float TOW_PI = PI * 2.0f;

int main()
{
    
    
	void (*fun)(float);//函数指针
	A(PI);
	fun = A;//函数指针指向A
	fun(PI);//函数指针调用

	fun = B;//函数指针指向B
	fun(TOW_PI);//函数在指针调用
	fun(13.0);//函数指针调用

	fun = C;//函数指针指向C
	C(PI);//函数指针调用
	return 0;
}

operation result:

insert image description here

In the above example program, a function pointer of void type is declared:

void (*fun)(float);//void类型的函数指针

During the running of the main function, the pointer is made to point to functions A, B and C respectively through the assignment statement, and then the function is called through the function pointer.

Guess you like

Origin blog.csdn.net/NuYoaH502329/article/details/132105533