(*(void(*)())0)() C code interpretation

#include<stdio.h>


int main()
{     (*(void(*)())0)();     //1, 0: represent an address     //2, void(*)(): represent a function pointer type. (void(*)())0: convert the 0 address into a function pointer type     //3, *(void(*)())0: represent the (void(*)())0 function pointer dereference.     //4, (*(void(*)())0)(): represents the function pointer that calls the dereference (that is, the calling function)     //This code comes from "C Traps and Defects"





    void(*signel(int, void(*)(int)))(int);
    // signel is a function name, and the parameters of the function are (integer and void(*)(int)) void(*)(int ): is a function type
    // void(*signel)(int) is a function pointer.
    //So this is a function declaration of signel.


    //Type redefinition
    typedef unsigned int u_int; //Redefine unsigned int type as u_int

    typedef void(* pfun_t)(int); // redefine void(*)(int) function pointer type as pfun_t


    void(*signel(int, void(*)(int)))(int); //equivalent to the next sentence
    pfun_t signel(int, pfun_t); //equivalent to the previous sentence

    return 0;
}


 array of function pointers

#include<stdio.h>

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

int Sub(int x, int y)
{
    return x - y;
}

int main()
{     int(*pf1)(int, int) = Add;     int(*pf2)(int, int) = Sub;     int(*p_arr[2])(int, int) = { pf1,pf2 } ; //p_arr is the array name of the function pointer array     return 0; }




#include<stdio.h>


int main()
{
	(*(void(*)())0)();
	//1、0:代表一个地址
	//2、void(*)(): 代表一种函数指针类型。 (void(*)())0 :把0地址强制转换成了一种函数指针类型
	//3、 *(void(*)())0 : 代表把(void(*)())0函数指针解引用。
	//4、(*(void(*)())0)() :代表调用解引用的函数指针(也就是调用函数)
	//此代码出自《C陷阱和缺陷》

	void(*signel(int, void(*)(int)))(int);
	// signel 是一个函数名,函数的参数是(整型和void(*)(int))   void(*)(int):是一个函数类型
	// void(*signel)(int)是一个函数指针。
	//所以说这是一个signel的函数声明。


	//类型重定义
	typedef unsigned int u_int;   //把unsigned int类型重新定义为 u_int

	typedef void(* pfun_t)(int);  // 把void(*)(int)函数指针类型重新定义为 pfun_t


	void(*signel(int, void(*)(int)))(int);  //和下一句等价
	pfun_t signel(int, pfun_t);             //和上一句等价

	return 0;
}


 函数指针数组

#include<stdio.h>

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

int Sub(int x, int y)
{
	return x - y;
}

int main()
{
	int(*pf1)(int, int) = Add;
	int(*pf2)(int, int) = Sub;
	int(*p_arr[2])(int, int) = { pf1,pf2 };  //p_arr 就是函数指针数组的数组名
	return 0;
}

Guess you like

Origin blog.csdn.net/xingyuncao520025/article/details/131887808