I try to wrap multiple struct
s in an union
and access the structure using the macro SET
. This macro sets the structure's field based on the given type t
, which is an enum
constant.
When I compile the below code with clang 12.0.0 I see the following -Wliteral-conversion
warning:
union.c:46:19: warning: implicit conversion from 'float' to 'int' changes value from 1.3 to 1 [-Wliteral-conversion]
SET (num, FLOAT, 1.3f);
~~~~~~~~~~~~~~~~~^~~~~
union.c:31:14: note: expanded from macro 'SET'
u.ni.val = v;
~ ^
This behaviour occurs whenever I call SET
with float
values, for example,
SET (num, FLOAT, 1.3f);
However, the code compiles without warning when I write the switch
block from the definition of SET
to the main
function.
What am I doing wrong here?
/* Compile with:
* cc -Wall -std=c17
*/
#include <stdio.h>
enum numer_type { VOID, INT, FLOAT };
union Number {
struct {
int type;
} n;
struct {
int type;
int val;
} ni;
struct {
int type;
float val;
} nf;
};
#define SET(u, t, v) switch (t) {
case VOID:
u.n.type = t;
break;
case INT:
u.ni.type = t;
u.ni.val = v;
break;
case FLOAT:
u.nf.type = t;
u.nf.val = v;
break;
default:
puts ("Unknown");
}
int main (void)
{
union Number num;
SET (num, FLOAT, 1.3f);
return 0;
}
question from:
https://stackoverflow.com/questions/66065324/literal-conversion-warning-in-union-of-structs 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…