错误:'unary *'的无效类型参数(有'int')|

时间:2013-12-16 10:10:07

标签: c function pointers

#include <stdio.h>
int _area(), _vol(), (*fnptr)();// declare the functions and the function pointer here

_area(a,b)
int a, b;
{
    return (a*b); //The return value of _area after parameters are passed to it
}
_vol(fnptr,c) //engaging the function pointer as a parameter
int c;
{
    fnptr = _area(); //initializing the function pointer to function _area
    int k = (*fnptr)(8,9); // error occurs here
    return (k*c);
}

编译会产生错误,

  

:错误:'unary *'的无效类型参数(有'int')

3 个答案:

答案 0 :(得分:1)

**int k = (*fnptr)(8,9);** should be int **k = (*fnptr)(8,9);

答案 1 :(得分:0)

使用函数原型我相信代码应该是这样的:

#include <stdio.h>
int _area(int a, int b);

int _vol(int (*fnptr)(int, int), int);;// declare the functions and the function pointer here

int _area(int a, int b)
{
    return (a*b); //The return value of _area after parameters are passed to it
}

int _vol(int (*fnptr)(int, int),int c) //engaging the function pointer as a parameter
{
    fnptr = _area; //initializing the function pointer to function _area
    int k = fnptr(8,9); // error occurs here
    return (k*c);
}

答案 2 :(得分:-1)

尝试将fnptr = _area()更改为fnprt = _area。你想将fnptr指向区域函数的地址,而不是调用函数本身。