简体   繁体   English

为什么赋值运算符 = 不返回指针的值而是解引用值?

[英]Why assignment operator = doesn't return the value of pointer but the dereference value?

From What does an assignment return?任务返回什么? :

An assignment expression has the value of the left operand after the assignment赋值表达式在赋值后具有左操作数的值

and this code:这个代码:

#include <iostream>
using namespace std;

int main() {
    
    int a[5] = { 0,1,2 };
    int* a_ptr = a;
    int b = (*a_ptr++ = 3); //int *b won't compile
    cout << b << endl; //3
}

What is the left operand of = when evaluating (*a_ptr++ = 3) ?求值(*a_ptr++ = 3)=左操作数是什么?

What's the definition of an operand ?操作数的定义是什么? In my mind, an operand is an identifier or name which is aptr .在我看来,操作数是aptr的标识符或名称。

int b = (*a_ptr++ = 3); is grouped as int b = (*(a_ptr++) = 3);分组int b = (*(a_ptr++) = 3); . . Note that the parentheses are superfluous;注意括号是多余的; you could have written你可以写

int b = *a_ptr++ = 3;

which in many ways makes the result more obvious, since the right-to-left associativity of = is such that the 3 carries over to the value of b .这在许多方面使结果更加明显,因为=从右到左的结合性使得 3 延续到b的值。

a_ptr++ is an expression equal to a_ptr but it will point to the second element of the array a once the whole statement completes. a_ptr++是一个等于a_ptr的表达式,但一旦整个语句完成,它将指向数组a的第二个元素。 Since you don't make use of that incremented pointer, the ++ is a red-herring, so the statement simplifies to由于您不使用该递增指针,因此++是一个红鲱鱼,因此该语句简化为

int b = *a_ptr = 3;

whereupon it's clear that *a_ptr = 3 has the effect of setting the first element of the array a to 3 and is an expression equal to 3, which is assigned to b .因此很明显*a_ptr = 3具有将数组a的第一个元素设置为 3 的效果,并且是一个等于 3 的表达式,它被分配给b

The left operand is *a_ptr++ .左操作数是*a_ptr++ As per the operator precedence , it's evaluated as根据 运算符优先级,它被评估为

*(a_ptr++)

where the post-increment is sequenced as a side effect, after the execution of the statement.在执行语句之后,后增量作为副作用进行排序。 The value of the operand is the result of the statement.操作数的值是语句的结果。 So, it's equivalent to所以,它相当于

 int b = (*a_ptr = 3);
 a_ptr++;

That said, in general, Operands are expressions or values on which an operator operates or works.也就是说,一般来说,操作数是运算符对其进行操作或工作的表达式或值。 So, it can be所以,可以

  • a variable (ex: var , as in int var )一个变量(例如: var ,如int var
  • a literal ( 5 or '"Hello"')文字( 5或 '"Hello"')
  • an expression (like *a_ptr++ )一个表达式(如*a_ptr++

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 重载赋值运算符:如果我们返回(* this),它是指针/地址处的值,那么赋值运算符的正确语法是什么? - Overloading assignment operator: if we return (*this), which is a value at a pointer/address, what's right syntax for assignment operator? 为什么这个赋值运算符不返回复制的 object? - Why doesn't this assignment operator return a copied object? 如果我取消引用空指针,为什么操作系统不会崩溃? - Why doesn't the OS crash if I dereference a null pointer? 在 GDB 中取消引用双指针(指向值的指针的指针) - Dereference a double pointer (a pointer to a pointer to a value) in GDB 为什么解除引用运算符(*)也用于声明指针? - Why is the dereference operator (*) also used to declare a pointer? 如果不使用单个解除引用运算符,指针指针的打印值是多少? - What value does a Pointer to a Pointer print if not a single dereference operator is used at all? 当取消引用(运算符*)按值返回时,箭头运算符(operator-&gt;)返回类型 - arrow operator (operator->) return type when dereference (operator*) returns by value 赋值运算符创建指针,无法删除 - Assignment operator creates pointer and doesn't make deleting possible 定义不返回值的C ++转换运算符 - Defining a C++ conversion operator that doesn't return a value 取消引用运算符不起作用(语法问题?) - Dereference operator doesn't work (syntax issue?)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM