In-depth analysis of pointers "Seven" (knowledge about function pointers)

  Personal homepage: Welcome everyone --> Populus euphratica under the desert

 Guys, you are so beautiful

 If you find the article helpful

 You can support bloggers with one click

 Every ounce of your concern is the driving force for me to persevere

 

 ☄: Focus of this issue: Contents related to function pointers

  I hope you all have a happy study and work every day. 

content

Why do functions have addresses?

What is the function address?

function pointer

So how do you call a function using a function pointer 

Method summary of function call:

A calling instance of a function pointer, used as a callback function

complex function pointer

Next notice:


Why do functions have addresses?

First of all, everyone knows that variables have addresses, so does functions have addresses ? Or what is the address of the function? Take a look at the diagram below:

 First of all, the function is also a part of the code, which means that the function should also be read into the CPU, so the address of the function is generated for the convenience of the CPU to read.

What is the function address?

First of all, there are two ways to represent the address of a function:

1. Direct function name      2. Take address function name:

Both of these represent the address of the function.

 Let's take a closer look through the assembly:

 

 Here it can be seen that the function has an address, and the address will be accessed by the CPU.

function pointer

As mentioned above, functions have addresses, so how to store function addresses? What to receive?

Of course , it is received with a function pointer . The following is the form of a function pointer:

void Fan()
{
	printf("胡杨树下\n");
}

int main()
{
	Fan();
	void(*p)() = Fan;
	return 0;
}

The type can be seen by debugging:

Fan here can also use &Fan.

So how do you call a function using a function pointer 

 As shown in the figure, this is a schematic diagram of a function call. First, dereference the function pointer variable, and then add parameters to the back to complete the purpose of using the function pointer to call the function.

Method summary of function call:

void Test(int a)
{
	static i = 0;
	printf("第%d次调用,a = %d\n",++i,a);
}

int main()
{
	int a = 11223344;
	Test(a);

	void(*p)(int) = &Test;
	void(*q)(a) = Test;

	q(a);
	(*p)(a);

	return 0;
}

 The first time is the case of a normal call, printing a.

The second time is called through the q function pointer, and it is initialized by putting &Test into it.

The third time is called through the p function pointer, and the initialization is put into Test for initialization.

It can be seen here that both parameter names and type names can be passed .

It is recommended to use the function name as the address directly, instead of the & address array name, also use the function name in the initialization of the function pointer, use p() when using the function pointer as a function, and do not use the dereference form.

void Test(int a)
{
	static i = 0;
	printf("第%d次调用,a = %d\n",++i,a);
}

int main()
{
	int a = 11223344;
	Test(a);

	//void(*p)(int) = &Test;//不推荐
	void(*q)(a) = Test;

	q(a);
	//(*p)(a);//不推荐

	return 0;
}

A calling instance of a function pointer, used as a callback function

Function pointer, the scope of use is limited, take an example as a callback function to understand:

void Scd()
{
	printf("输入有效\n");
}

void Fan(void(*p)())
{
	int a = 0;
	scanf("%d", &a);
	if (a != 0)
	{
		p();
	}
}

int main()
{
	Fan(Scd);
	return 0;
}

The logic of this function is to call the Fan function, pass the parameter as a function pointer, input a number, if it is not 0, call the Scd function and print "input valid".

complex function pointer

First let's look at a classic example:

(*(void(*)())0)();

First of all we should analyze from the outside, first void(*) () is a function pointer (function pointer) 0 is a cast, then (*(function pointer) 0) () this is a function pointer again.

So the whole is to convert the 0 address into a function pointer and call it.

Then there are more complex function pointer arrays and pointers to function pointer arrays that are not explained, but the understanding method is the same, and it is also related to the knowledge of function pointers and pointer arrays.

In addition, when analyzing any complex pointer or array, remove the variable name, which is the type , and then analyze it.

Next notice:

The next issue will explain the knowledge related to dynamic development

The next issue will be more exciting~! ~! ~!

Guess you like

Origin blog.csdn.net/m0_64770095/article/details/124281235