预期'voidpc'但参数类型为'double'

时间:2014-06-11 12:29:34

标签: c linux zlib

builtin-stat.c: In function ‘abs_printout’:
builtin-stat.c:1023:5: error: incompatible type for argument 2 of ‘gzwrite’
     gzwrite(trace_file, avg, sizeof(avg));
     ^
In file included from builtin.h:6:0,
                 from builtin-stat.c:45:
/usr/include/zlib.h:1341:21: note: expected ‘voidpc’ but argument is of type ‘double’
 ZEXTERN int ZEXPORT gzwrite OF((gzFile file,

什么是voidpc类型?从来没有听说过。 zlib.h告诉我它是z_void。这是什么意思?我应该在这里进行类型转换吗?

警告:

 CC       builtin-stat.o
builtin-stat.c: In function ‘abs_printout’:
builtin-stat.c:1020:5: warning: ISO C90 forbids mixed declarations and code [-Wdeclaration-after-statement]
     int written = gzwrite(trace_file, (void *) &avg, sizeof(avg));
     ^
builtin-stat.c:1024:9: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘double’ [-Wformat=]
         fprintf(stderr, "Failed to write %d to file\n", avg);
         ^
builtin-stat.c:1026:13: warning: passing argument 1 of ‘fclose’ from incompatible pointer type [enabled by default]
             fclose(trace_file);
             ^
In file included from util/util.h:45:0,
                 from builtin.h:4,
                 from builtin-stat.c:45:
/usr/include/stdio.h:237:12: note: expected ‘struct FILE *’ but argument is of type ‘gzFile’
 extern int fclose (FILE *__stream);
            ^
builtin-stat.c:1029:5: warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘long unsigned int’ [-Wformat=]
     printf("Passed %d bytes, wrote %d\n", sizeof avg, written);

1 个答案:

答案 0 :(得分:1)

voidpc是typedef:typedef void const * voidpc。您可以在zconf.h头文件中找到zlib typedef等,而zlib.h标题又需要这样。
source of zconf.h here

我认为,这里的类型转换不会成功。该错误表明avg属于double类型。 gzwrite获取未压缩的数据,按字节每字节处理并将多个字节写入目标文件。指针允许gzwrite转换为char *,并轻松设置其业务。只需传递指针,可选择将其转换为voidpcvoid *,然后_检查返回值:

int written = gzwrite(
    trace_file,
    (void *) &avg,
    sizeof(avg)
);
if (written == -1)
{
    fprintf(stderr, "Failed to write %f to file\n", avg);
    if (trace_file)
        gzclose(trace_file);
    exit( EXIT_FAILURE );
}
printf("Passed %llu bytes, wrote %d\n", sizeof avg, written);

应该做的伎俩。