首页 > 解决方案 > 当第一个参数是结构类型时,C 中的函数声明问题

问题描述

我正在使用由其他人编写的用于某种目的的 C 代码。编译程序时:

警告:函数“shine”的隐式声明 [-Wimplicit-function-declaration

我在下面给出部分代码:

struct attribute
{
    int type, symb;      
    float lower, upper;
} examples[MaxExs][MaxAtts];

下面是在 func1() 内部调用 Shine() 的代码。

int func1()
{
    int e, r, nstrule;
    float lwstdist;
    ...
    shine(examples[e], &nstrule, &lwstdist, e);
    ...
}

下面是shine() 的函数定义。

int shine(ex, nstrule, lwstdist, leftout) 
struct attribute ex[];
int *nstrule, leftout;
float *lwstdist;
{
    ...
}

我只想删除该警告。所以我想在代码的开头声明它。我尝试使用:

int shine(xxx, int*, float*, int);

我不知道我做的是否正确以及用什么来代替 xxx。我是C的新手。所以我无法弄清楚。如果我能获得一些材料来完成这个特定的事情,那么除了这个帮助之外,这将是一个很大的帮助。

标签: cfunctionfunction-declaration

解决方案


您需要放置一个前向声明struct attribute

struct attribute;
int shine(struct attribute[], int*, float*, int);

顺便提一句。未命名的函数参数不是标准的 C。尽管许多编译器都接受它,因为它在 C++ 中是允许的并且实现起来很简单。

此功能将添加到即将发布的 C23 标准中的 C 中。


推荐阅读