c - 来自不兼容指针类型的警告初始化 - C 函数指针数组

标签 c function pointers initialization function-pointers

我无法解决这个警告

这是我的原型(prototype)

int doubeInput (int* inputVal);
int squareInput (int* inputVal);
int cubeInput (int* inputVal);

出错的行是

int (*funPtrArr[])(int)={doubleInput, squareInput, cubeInput};

这条线在主线

while (choice >0 && choice <=3)
    {
        (*funPtrArr[choice])(inputVal); 

    }

这是我的功能

//function to perform the menu option 1,user input passed in
int doubleInput (int* inputVal)
{
    //calculate double
    int answer = (*inputVal) *= (2);
    printf("%d x 2", inputVal); 

    return (answer);
}


//function to perform the menu option 2,user input passed in
int squareInput (int* inputVal)
{
    //calculate square
    int answer = (*inputVal) *= (*inputVal);
    printf("%d x %d", inputVal, inputVal);

    return (answer);

}


//function to perform the menu option 3,user input passed in
int cubeInput (int* inputVal)
{
    //calculate cube
    int answer = (*inputVal) *= (*inputVal) *= (*inputVal);
    printf("%d ^ 3", inputVal);

    return (answer);
}

我不断收到此错误,我是 C 的新手。我的功能尚未完成,因为我无法测试它们。 请帮忙

最佳答案

int (*funPtrArr[])(int) 

声明了一个指针数组,指向带有 int 参数的函数,但您的函数有一个 int * 参数。 所以该行应该是

int (*funPtrArr[])(int *)={doubleInput, squareInput, cubeInput};

关于c - 来自不兼容指针类型的警告初始化 - C 函数指针数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58417160/

相关文章:

javascript - 在页面的不同点添加类/ID

带括号的 C++ 非函数 typedef

javascript - jQuery:在事件处理程序的无名函数中调用函数

c# - 如何将 float 组从 c# 编码到 c,然后在 c 中编辑数组,同时让 c# 能够立即引用更新后的数组

c - 如何用两个函数编写 C 程序?

objective-c - 我的 CRC-12 实现有什么问题?

c - 序列化:char 和 uint32_t 的大小

c++ - 将链表复制到对链表的引用

c++ - char* 不保存内存地址

C 无垃圾 char 指针