【发布时间】:2021-06-07 05:35:32
【问题描述】:
我知道如何简单地避免lvalue required as unary ‘&’ 错误(如here)
我尝试做的是避免在一行中出现此错误。 为什么?我定义了很多 const,并且在我正在处理的一些写得不好的代码中,这些 const 用于初始化。我不想每次使用时都定义一个中间变量...
在下面的例子中我需要中间值't'
int * p;
const int q = 88;
int t = (int)q; //works but I have to add for each time I used q, an intermediate variable.
p = &t;
printf("p: %u\n",*p);
在以下示例中,我不需要中间值 't'(但它不起作用)
int * p;
const int q = 88;
p = &((int)q); //what I would like to do but raises: error: lvalue required as unary ‘&’ operand
printf("p: %u\n",*p);
有单线方式吗? 使用#defines 可能是一个提示...
注意:我不希望在我的编译中出现任何警告,因为我正在纠正 MISRA 违规:
Cast from 'const int *' removes 'const' qualifier (MISRAC2012-RULE_11_8.a)
注意:p 没有声明为 const int,因为 p 稍后会被修改。 p 使用 i 来初始化自己。
如果变量很简单,则“StoryTeller - Unslander Monica”给出的解决方案有效:p = &((int){i});
但这不适用于结构:
typedef struct
{
int i;
int j;
}struct_t;
const struct_t q = {.i=12,.j=88};
printf("q: %u - %u\n",q.i,q.j);
struct_t * p = &((struct_t){q});
printf("p: %u - %u\n",p->i,p->j);
其中的编译引发error: incompatible types when initializing type ‘int’ using type ‘struct_t {aka const struct }’
【问题讨论】:
-
你必须转换
&的结果...(int *)(&i) -
不,它会引发 MISRA 错误
-
这个问题缺少一个重要的细节:为什么
p没有声明const int *? 这样可以以正确的方式解决问题。 -
@GuillaumeD 您是稍后修改
p,还是*p?如果是前者,可以定义为const int *。 -
@GuillaumeD,也可以为结构类型形成复合文字。事实上,这是他们的主要用例。对术语(“复合文字”)进行一些研究。这一切确实有相当强烈的代码气味,但你没有提供足够的东西让我们提出更好的替代方案。
标签: c pointers casting initialization constants