gpt4 book ai didi

c - 指向引用函数的指针数组的指针,如何更改指针值和调用函数?

转载 作者:太空宇宙 更新时间:2023-11-04 02:23:13 26 4
gpt4 key购买 nike

int *(*(*functions_array)[4](); 
int *function() { /*code */ };

我知道使用指针引用函数 int *(*func)(); 我可以将函数的指针值分配给 func func = &function;然后用 func(); 调用。

如果添加数组,我最接近这种行为的是什么?

最佳答案

你可能想要这个:

#include <stdio.h>

int function1(void)
{
printf("function 1\n");
return 1;
};

int function2(void)
{
printf("function 2\n");
return 2;
};

int(*FunctionPointer)(void); // a function pointer
int(*ArrayOfFunctionPointers[4])(void); // an array of 4 function pointers


int main()
{
FunctionPointer = function1;
FunctionPointer(); // will call function1

FunctionPointer = function2;
FunctionPointer(); // will call function2

ArrayOfFunctionPointers[0] = function1;
ArrayOfFunctionPointers[1] = function2;

for (int i = 0; i < 2; i++)
{
ArrayOfFunctionPointers[i]();
}
}

输出:

function 1
function 2
function 1
function 2

关于c - 指向引用函数的指针数组的指针,如何更改指针值和调用函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54861389/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com