【问题标题】:warning initialization from incompatible pointer type - C array of function pointers来自不兼容指针类型的警告初始化 - C 函数指针数组
【发布时间】:2019-10-16 15:45:05
【问题描述】:

我无法解决此警告

这是我的原型

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

有错误的行是

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

这一行在main

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 doubeInput (int* inputVal); 必须是 doubleInput

标签: c function pointers initialization function-pointers


【解决方案1】:
int (*funPtrArr[])(int) 

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

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

【讨论】:

  • 好的,确实摆脱了这些错误,但现在我得到另一个 [警告] 传递 'funPtrArr[choice]' 的参数 1,使指针从整数而不进行强制转换。
  • main() 中inputVal 的类型是什么?如果是int,则必须通过&amp;inputVal
猜你喜欢
  • 2013-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多