[英]What is the difference between the two syntax?
Why 为什么
(a?b:c)=5;
in 'C' shows lvalue required while 在“ C”中显示所需的左值,而
*(a?&b:&c)=5;
is perfectly fine? 很好吗? What is the difference between the two?
两者有什么区别?
Assuming a=1, for first case, it gives b=5 and second case it gives, *(&b)=5
.` 假设a = 1,对于第一种情况,它给出b = 5,而对于第二种情况,它给出
*(&b)=5
。
What i am not able to understand is: what difference does it make if we write b=5
or *(&b)=5
? 我不明白的是:如果我们写
b=5
或*(&b)=5
会产生什么区别?
what difference does it make if we write b=5 or *(&b)=5?
如果我们写b = 5或*(&b)= 5,会有什么区别?
The second one gets a pointer to b
and then dereferences that pointer, storing 5 into the obtained pointer. 第二个获得指向
b
指针,然后取消对该指针的引用,将5存储到获得的指针中。
However, your question seems to be ignoring the real question: why it works in the second case but not the first. 但是,您的问题似乎忽略了真正的问题:为什么它在第二种情况下有效,但在第一种情况下不起作用。 And that has to do with expressions in C and how they're dealt with.
这与C语言中的表达式及其处理方式有关。
The result of the ?:
operator is a value; ?:
运算符的结果是一个值; specifically, it is an rvalue . 具体来说,它是一个右值 。 Loosely defined, an rvalue is a value that can't go on the left-hand side of an assignment.
宽松定义的值是不能在赋值左侧出现的值。 They are so named because they're values that go on the right-hand side of an assignment.
之所以命名它们,是因为它们是赋值在赋值右侧的值。 For example, the expression "5" is an rvalue expression.
例如,表达式“ 5”是右值表达式。 You can't do
5 = 40;
你不能做
5 = 40;
that's nonsense. 废话
A named variable is an lvalue. 命名变量是左值。 You can put an lvalue on the left-hand side of an equation.
您可以将左值放在等式的左侧。 The expression
a
is an lvalue expression (assuming that a
is a variable name that is in scope). 表达式
a
是一个左值表达式(假设a
是范围内的变量名)。
However, the expression (a + b)
is an rvalue expression , not an lvalue. 但是,表达式
(a + b)
是一个右值表达式 ,而不是左值。 And with good reason; 并且有充分的理由; you can no more do
(a + b) = 30;
你不能再做
(a + b) = 30;
than you could (5 + 10) = 30;
比你可能的
(5 + 10) = 30;
. 。
The result of the ?:
operator is an rvalue expression, just as with the +
operator above. 和上面的
+
运算符一样, ?:
运算符的结果是一个右值表达式。 This explains why (a?b:c) = 5;
这解释了为什么
(a?b:c) = 5;
doesn't work; 不起作用; you're trying to assign a value to an rvalue expression.
您正在尝试为右值表达式分配值。 That's illegal.
那是非法的。
Now, let's look at the second case. 现在,让我们看第二种情况。 We know that
?:
results in an rvalue expression. 我们知道
?:
导致一个右值表达式。 Now, that explains what the classification of the expression is, but what about the type of the expression's result? 现在,这说明了表达式的分类是什么,但是表达式结果的类型又如何呢? Well, assuming that
b
and c
are both int
s, the type of (a?b:c)
is also int
. 那么,假设
b
和c
均为int
S, 类型 (a?b:c)
也被int
。
However, when you do (a?&b:&c)
, the type of this expression is int*
, not int
. 但是,当您执行
(a?&b:&c)
,此表达式的类型为int*
,而不是int
。 It is a pointer to an integer. 它是指向整数的指针 。 It's an rvalue expression of type "pointer to
int
." 这是类型为“指向
int
指针”的右值表达式。 It will return either the address of b
, or the address of c
. 它将返回
b
的地址或c
的地址。
When you have an int*
, and you dereference it with the *
operator, what do you get? 当您拥有一个
int*
,并且使用*
运算符对其进行取消引用时,您会得到什么? You get an lvalue expression of type int
. 您将得到一个类型为
int
的左值表达式 。 Since (a?&b:&c)
is an rvalue expression of type int*
, if you dereference it, you will get an lvalue expression of type int
. 由于
(a?&b:&c)
是int*
类型的右值表达式,因此,如果取消引用它,将得到int
类型的左值表达式。 This lvalue will refer to either b
or c
, depending on the contents of a
. 此左值将是指任一
b
或c
,取决于内容a
。
To put it simply, it works exactly like this: 简单地说,它的工作原理如下:
int *ptr = NULL;
if(a)
ptr = &b;
else
ptr = &c;
*ptr = 5;
You get a pointer, which could point to any particular memory location, then you store something in the location being pointed to. 您将获得一个指针,该指针可以指向任何特定的内存位置,然后在指向的位置中存储一些内容。 It's that simple.
就这么简单。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.