gpt4 book ai didi

c - C 中的快速 ifs 和指针发出警告 : argument to '&' not really an lvalue; this will be a hard error in the future

转载 作者:行者123 更新时间:2023-12-02 04:44:10 25 4
gpt4 key购买 nike

我有以下结构:

typedef struct {
float x;
float y;
} point;

我从标准输入中填充它如下:

point pts[3];

int i, j;
for(i = 0; i < 3; i++)
for(j = 0; j < 2; j++)
scanf( "%f", &(j ? pts[i].y : pts[i].x) );

但是编译器 (GCC) 给我这个警告:

warning: argument to '&' not really an lvalue; this will be a hard error in the future

错误中给出的行是:

scanf( "%f", &(j ? pts[i].y : pts[i].x) );

这样做有什么问题?是否有使用快速 if 来执行此操作的捷径?

最佳答案

条件表达式不返回一个可赋值的值,所以你不能取它的地址。但是,您可以对地址进行条件设置,如下所示:

scanf( "%f", (j ? &pts[i].y : &pts[i].x) );

现在条件在两个地址表达式之间进行选择,因此结果是一个有效地址。

关于c - C 中的快速 ifs 和指针发出警告 : argument to '&' not really an lvalue; this will be a hard error in the future,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20165265/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com