获取您的代码并从中制作SSCCE,如下所示:
#include <stdlib.h>
struct RTIME { int a; int b; };
typedef const struct RTIME RTIME;
RTIME *rtCreate(void)
{
RTIME *rtime;
rtime = malloc(sizeof(*rtime));
if (rtime != NULL)
{
/* Initialization stuff */
}
return rtime;
}
void rtDestroy(RTIME **rtime)
{
if (*rtime != NULL)
{
free(*rtime);
*rtime = NULL;
}
}
使用 GCC 4.7.1 和命令行编译:
$ gcc -O3 -g -std=c99 -Wall -Wextra -Wmissing-prototypes -c mf.c
mf.c:6:8: warning: no previous prototype for ‘rtCreate’ [-Wmissing-prototypes]
mf.c:20:6: warning: no previous prototype for ‘rtDestroy’ [-Wmissing-prototypes]
mf.c: In function ‘rtDestroy’:
mf.c:24:9: warning: passing argument 1 of ‘free’ discards ‘const’ qualifier from pointer target type [enabled by default]
In file included from mf.c:1:0:
/usr/include/stdlib.h:160:7: note: expected ‘void *’ but argument is of type ‘const struct RTIME *’
$
省略const
并且您只会收到有关缺少原型的(有效)警告。
我猜你正在使用旧版本的 GCC(因为旧版本不包含该note:
行中的额外信息),并且不知何故你的typedef
forRTIME
包含一个const
.
作为一般规则,您不需要const
a typedef
,但该规则肯定会有例外。
从编辑过的问题中可以看出,限定词是volatile
而不是const
. 当typedef
我的示例代码更改时,GCC 4.7.1 说:
mf.c:6:8: warning: no previous prototype for ‘rtCreate’ [-Wmissing-prototypes]
mf.c:20:6: warning: no previous prototype for ‘rtDestroy’ [-Wmissing-prototypes]
mf.c: In function ‘rtDestroy’:
mf.c:24:9: warning: passing argument 1 of ‘free’ discards ‘volatile’ qualifier from pointer target type [enabled by default]
In file included from mf.c:1:0:
/usr/include/stdlib.h:160:7: note: expected ‘void *’ but argument is of type ‘volatile struct RTIME *’
当我使用系统 GCC 编译时,我得到一个更简单、不太精确的错误消息:
mf.c:7: warning: no previous prototype for ‘rtCreate’
mf.c:21: warning: no previous prototype for ‘rtDestroy’
mf.c: In function ‘rtDestroy’:
mf.c:24: warning: passing argument 1 of ‘free’ discards qualifiers from pointer target type
这是来自 Apple 的 GCC:
i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)
因此,限定词是volatile
而不是const
.
尝试升级到 GCC 4.7.x 的一个很好的理由是错误消息比早期版本有了很大改进。改进的消息也在 4.6.0 中;4.5.2 中的消息是较旧的样式,信息量较少的消息。