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

最佳答案

voidpc是一个类型定义:typedef void const * voidpc。您可以在zconf.h头文件中找到zlib typedefs等,而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);

应该会成功的。

关于c - 预期为“voidpc”,但参数的类型为“double”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24162979/

10-12 18:11