c - 错误 : aggregate value used where an integer was expected

标签 c linux printf unions format-specifiers

我有以下联盟

union data {
     uint64_t val;
     struct{
     ....
     }
};

我有一个函数

func(union data mydata[])
{
    printf("%llu",(uint64_t)mydata[0]); // Here is the error
}

当我编译这段代码时出现以下错误

error: aggregate value used where an integer was expected

最佳答案

您无法访问索引 union 数组的字段:mydata[0]union data 类型的值,无法转换为 uint64_t

您需要访问正确的 union 成员:

printf("%" PRIu64, mydata[0].val);

选择 uint64_t 值。不需要类型转换。

此外:使用 PRIu64 可移植地打印 64 位值,您不能假设 %llu 是正确的格式说明符。

关于c - 错误 : aggregate value used where an integer was expected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20994959/

相关文章:

c - 在 C 程序中使用汇编语言的目的是什么?

c - 从16位地址中划分高/低字节的方法?

c - 小屏幕C/C++图形库

c - sprintf() 段错误

c - printf() 中的 scanf() 如何工作?

C : confusion over a small piece of code - what is '&0x' ?

java - 如何制作 Java 跨平台 GUI 应用程序(Windows、Linux)?我应该使用什么工具?

c 套接字文件传输,服务器不打开现有文件

c - 如何在Linux中使用C检测系统是否要待机

internet-explorer-9 - 白色背景 div 在 IE9 打印预览中是透明的