简体   繁体   English

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

[英]error: invalid type argument of 'unary *' (have 'int')|

#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);
}

Compiling produces an error , 编译会产生错误,

:error: invalid type argument of 'unary *' (have 'int') :错误:'unary *'的无效类型参数(有'int')

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

Using function prototypes I believe the code should look something like this: 使用函数原型我相信代码看起来像这样:

#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);
}

Try to change fnptr = _area() to fnprt = _area . 尝试将fnptr = _area()更改为fnprt = _area You want to point fnptr to the adress of _area function, not to call the function itself. 你想将fnptr指向_area函数的地址,而不是调用函数本身。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM