是* P是左值或右值 [英] Is *p an lvalue or rvalue
问题描述
在以下code,是 *一个
右值或左值?
In the following code, is *a
an rvalue or an lvalue?
#include <stdio.h>
void main()
{
int b=2;
int *a=NULL;
a=&b;
*a=3;
printf("%d",*a);
}
推荐答案
有一个左值,并且可以在r值的上下文中使用。见例子;
It is an l-value and can be used in a context of r-value. See the example;
int i, j, *p, *q;
p = &i;
q = &j;
*p = 1; // *p is l-value
*q = 2; // *q is l-value
*p = *q // *q is r-value
由于指向的查尔斯·贝利在他的评论: - 子前pression * q
仍然是一个左值,它经历了一个L值r值转换,因为它是在上下文中使用,其中的R值是需要。
As pointed by Charles Bailey in his comment:- The sub-expression *q
is still an l-value, it undergoes an l-value to r-value conversion because it is used in a context where an r-value is required.
现在人们可能会想到的为什么 * Q
仍然是左值?结果
因为R值的定义 R值是不持续时间超过使用它的前pression临时值。的结果
在的这种情况下,值* Q
是持续超过前pression * p = * Q
。
Now one may think Why *q
is still l-value?
Because by definition of r-value an r-value is a temporary value that does not persist beyond the expression that uses it.
In this case value of *q
is persists beyond the expression *p = *q
.
注意:
*操作符 - 的差额/间接运算符 - 对指针变量进行操作,并在指针地址返回 L值等同于值
" * " operator--the deference/indirection operator -- operates on pointer variable, and returns an l-value equivalent to the value at pointer address.
这篇关于是* P是左值或右值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!