printf格式字符串警告长无符号整数

Ahm*_*kol 0 c linux

我的c代码有问题,当我尝试printf相同的值时,g ++编译器给出了一个关于long unsigned int的错误.这是我的代码和我得到的错误:

as3.c: warning: format %s expects type char *, but argument 2 has type onion
as3.c: warning: format %d expects type int, but argument 3 has type long unsigned int
as3.c: warning: format %d expects type int, but argument 3 has type long unsigned int
as3.c: warning: format %d expects type int, but argument 5 has type long unsigned int
as3.c: warning: format %d expects type int, but argument 2 has type long unsigned int
Run Code Online (Sandbox Code Playgroud)
#define NUM1 5.557111111111111
#define NUM2 1937006915
#define NUM3 1668248096
#define NUM4 8555

#include <stdio.h>

typedef union {
char  * a;
double num;
int * i;
}onion;

int main(int argc, char ** argv)
{
onion myoni;
char array[] = "TESTING";
int array2[] = { NUM2, NUM1, NUM3 };
myoni.a = array; 
printf("char: %s, %d\n",myoni,sizeof(myoni.a));
myoni.num = NUM1; 
printf("double: %10.15f, %d\n", myoni.num, sizeof(myoni.num));
myoni.i = array2;
printf("int: %d %d %d, %d\n", myoni.i[0], myoni.i[1],myoni.i[2], sizeof(myoni.i));

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

oua*_*uah 5

代替:

printf("char: %s, %d\n",myoni,sizeof(myoni.a));
Run Code Online (Sandbox Code Playgroud)

使用:

printf("char: %s, %zu\n",myoni.a,sizeof(myoni.a));
Run Code Online (Sandbox Code Playgroud)

也就是说,传递一个char *for s转换说明符并使用%zu转换规范来获取sizeof运算符的结果.