【问题标题】:expected ‘voidpc’ but argument is of type ‘double’预期为“voidpc”,但参数类型为“double”
【发布时间】:2014-06-11 12:29:33
【问题描述】:
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);

【问题讨论】:

    标签: c linux zlib


    【解决方案1】:

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

    我认为,这里的类型转换不会解决问题。错误是说avg 是double 类型。 gzwrite 获取未压缩的数据,逐字节处理,并将多个字节写入目标文件。指针允许gzwrite 转换为char *,并轻松开展业务。只需传递一个指针,可选择将其转换为voidpc 或void *,然后_检查返回值:

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

    应该做的伎俩。

    【讨论】:

    • 是的,当我将其更改为 gzwrite(trace_file, &avg, sizeof(double));没有错误。但它没有被写入文件。
    • @pistal:返回值是多少? gzwrite 返回写入的字节数,错误时返回 -1
    • 如何查看返回值?
    • @pistal:在我刚才的回答中添加了这个:gzwrite 返回一个 int,表示错误(-1 => 出了问题),或者写入文件的字节数
    • 传递 8 个字节,写入 8 个。但文件大小为零。不明白为什么!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-31
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多