当前位置:网站首页>C (pointer-02)

C (pointer-02)

2022-06-10 19:10:00 Fate friend I

Make progress every day , Make progress !! Thank myself

 Insert picture description here

1
(* ( void(*)() ) 0 )
//void(*)()- Function pointer type 
// hold 0 Force type to :void(*)()  Function pointer type  -0 Is the address of a function 
2
void(* signal( int ,  void(*)(int) )  )(int)
// Simplify the method 
typedef void(* pfun_t)(int);
pfun_t signal(int,pfun_t);
//signal It's a function declaration 
//signal The arguments to the function are 2 individual , The first is int, The second is function pointers , The parameter the function pointer points to is Int, The return type is void
//signal The return type of a function is also a pointer , The function parameter pointed to by this pointer is int, The return type is void

 Insert picture description here

🪬🪬 Function pointer array usage : Transfer table

int Add(int x,int y)
{
    
	int z=x+y;
	return z;
}
int main()
{
    
	int a=10;
	int b=10;
	int(*pa)(int,int)=Add;
	printf("%d\n",pa(2,3));
	printf("%d\n",Add(2,3));

	printf("%d\n",(*pa)(2,3));
	return 0;
}
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 *arr[4];
	int(*parr[4])(int ,int)={
    Add,Sub,Mul,Div};
	// An array of function pointers 
	int(*pArr[5])(int ,int);
	// A pointer to an array of function pointers 
	int(*(PPArr)[5](int,int))=&pArr;
	return 0;
}

practice

char* my_strcpy(cahr* dest,const char* src);
//1 Write a function pointer Pf, Can point to my_strcy
char*(*pf)(char*,const char*);
//2 Write an array of function pointers pfArr, Be able to store 4 individual my_strcy Address of function 
cha*(*pfarr[4])(cahr*,const char*);

Callback function :
A callback function is a function called through a function pointer , If you put a pointer to a function ( Address ) Pass as argument to another function , When this pointer is used to call the function it refers to , Let's just say this is a callback function . The callback function is not called directly by the function's implementer , It is called by another party when a specific event or condition occurs , Used to respond to the event or condition .

原网站

版权声明
本文为[Fate friend I]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206101815406472.html