Advanced pointer detailed 2

1. Function pointer
First look at a piece of code

#include<stdio.h>
void test()
{
    
    
	printf("hehe\n");
}
int main()
{
    
    
	printf("%p\n", test);
	printf("%p\n", &test);
}


The output is two addresses, these two addresses are the address of the test function, then we want to save the function address, how to save it?

First of all, to be able to store the address, you need a pointer variable to the function, that is, the function pointer

void (*fun1)();

fun1 is first combined with a pointer, indicating that fun1 is a pointer, the function pointed to by the pointer has no parameters, and the return value type is void

Let’s look at two interesting pieces of code

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

Coerce 0 into a function pointer of type void(*)(). 0 is the address of a function. After dereference, the function at address 0 is called

void (*signal(int ,void(*)(int)))(int)

First, signal is a function declaration. The function has two parameters, one is an integer, and a function pointer type
. The return value type of the function is to remove the function name and parameters, and the rest is the return value type, and the return value type of the function is also A function pointer type.
This code is a bit complicated, we can simplify it

typedef  void(* pfun_t)(int);
pfun_t signal(int ,pfun_t);

Use of function pointers

#include<stdio.h>
int add(int x, int y)
{
    
    
	int z = 0;
	z = x + y;
	return z;
}
int main()
{
    
    
	int a = 10;
	int b = 20;
	int(*pa)(int, int) = add;
	printf("%d\n", pa(2, 3));
	printf("%d\n", (*pa)(2, 3));

}

2. Function pointer array

We have already studied pointer arrays, such as int*arr[10].

If you store the address of a function in an array, this array is called an array of function pointers and is defined as follows:

int(*pa[10])(int ,int);

Pa is combined with [], indicating that pa is an array. What is the content of the array? Is a function pointer of type int(*)(int,int)

Thinking:

char*my_strcpy(char*dest,const char*src);

1. Write a function pointer that can point to my_strcpy
2. Write a function pointer array that can store the addresses of 4 my_strcpy functions

char*(*p)(char* ,const char*);
char*(*p[4])(char* ,const char*);

Function pointer array purpose: transfer table
Example: calculator

#include<stdio.h>
int add(int x, int y)
{
    
    
	return x + y;
}
int sub(int x, int y)
{
    
    
	return x - y;
}
int mul(int x, int y)
{
    
    
	return x * y;
}
int div(int x, int y)
{
    
    
	return x / y;
}
int main()
{
    
    
	int x, y;
	int input = 1;
	int ret = 0;
	int(*p[5])(int, int) = {
    
     0,add,sub,mul,div };
	while (input)
	{
    
    
		printf("*******************************\n");
		printf("    1:add              2:sub   \n");
		printf("    3:mul              4:div   \n");
		printf("*******************************\n");
		printf("请选择:");
		scanf("%d", &input);
		if (input >= 1 && input <= 4)
		{
    
    
			printf("请输入操作数:");
			scanf("%d %d", &x, &y);
			ret = p[input](x, y);
		}
		else
			printf("输入有误\n");
		printf("ret=%d\n", ret);
	}
}

To be continued...

Guess you like

Origin blog.csdn.net/DR5200/article/details/112388873