简体   繁体   English

试图理解:对二进制表达式无效的操作数,C

[英]Trying to understand: invalid operands to to binary expression, C

Last line yields "invalid operands to binary expression". 最后一行产生“对二进制表达式无效的操作数”。 Trying to understand why. 试图了解原因。 Does it mean that "p2-p1" is an invalid operand to the binary expression "-" that lies to the right of p3? 这是否意味着“ p2-p1”是位于p3右侧的二进制表达式“-”的无效操作数? Any rule I can follow here? 我在这里可以遵循任何规则吗? Confusing to me because "3-2-1" integers are valid. 我感到困惑,因为“ 3-2-1”整数有效。

  int array[3] = {1,2,3};
  int* p1 = &array[0];
  int* p2 = &array[1];
  int* p3 = &array[2];

  p3-p2-p1;

You are doing address arithmetic. 您正在执行地址算术。 Given operator precedence, it is evaluating p1-p2-p3 as (p1-p2)-p3 . 给定运算符的优先级,它将p1-p2-p3评估为(p1-p2)-p3 p1-p2 yields not an address but an integer. p1-p2不是地址,而是整数。 Then you are attempting to subtract an address from an integer, which isn't valid. 然后,您尝试从整数中减去一个无效的地址。 You could do p1-(p2-p3) , then it's taking p2-p3 , yielding an integer, and subtracting that as an integer offset from an address ( p1 ), which will compile. 你可以做p1-(p2-p3) ,然后取p2-p3 ,得到一个整数,然后从要编译的地址( p1 )中减去它作为整数偏移量。 However, [Thanks to @EOF for this reference in his comment] such subtraction (of integer from a pointer) would only be valid if it points somewhere within the allocation for p1 . 但是,[感谢@EOF在其注释中提供此参考]这种减法(从指针中减去整数)仅在其指向p1分配内的某个位置时才有效。 It's subject to the C11 standard described specifically in section 6.5.6, excerpted below: 它受第6.5.6节中具体描述的C11标准的约束,摘录如下:

When an expression that has integer type is added to or subtracted from a pointer, the result has the type of the pointer operand. 将具有整数类型的表达式添加到指针或从指针中减去时,结果将具有指针操作数的类型。 If the pointer operand points to an element of an array object, and the array is large enough, the result points to an element offset from the original element such that the difference of the subscripts of the resulting and original array elements equals the integer expression. 如果指针操作数指向数组对象的元素,并且数组足够大,则结果指向与原始元素偏移的元素,以使结果数组元素和原始数组元素的下标之差等于整数表达式。 In other words, if the expression P points to the i-th element of an array object, the expressions (P)+N (equivalently, N+(P)) and (P)-N (where N has the value n) point to, respectively, the i+n-th and i−n-th elements of the array object, provided they exist. 换句话说,如果表达式P指向数组对象的第i个元素,则表达式(P)+ N(相当于N +(P))和(P)-N(其中N的值为n)表示到数组对象的第i + n个元素和第i-n个元素(如果存在)。 Moreover, if the expression P points to the last element of an array object, the expression (P)+1 points one past the last element of the array object, and if the expression Q points one past the last element of an array object, the expression (Q)-1 points to the last element of the array object. 此外,如果表达式P指向数组对象的最后一个元素,则表达式(P)+1指向数组对象的最后一个元素之后,如果表达式Q指向数组对象的最后一个元素之后,表达式(Q)-1指向数组对象的最后一个元素。 If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; 如果指针操作数和结果都指向同一数组对象的元素,或者指向数组对象的最后一个元素,则求值不会产生溢出; otherwise, the behavior is undefined. 否则,行为是不确定的。 If the result points one past the last element of the array object, it shall not be used as the operand of a unary * operator that is evaluated. 如果结果指向数组对象的最后一个元素之后,则不应将其用作被评估的一元*运算符的操作数。

In your code, p1 , p2 and p3 are all pointers to integers , not integers. 在您的代码中, p1p2p3都是指向整数的指针 ,而不是整数。

To get what you want, you probably want: 要获得想要的东西,您可能想要:

*p3 - *p2 - *p1;

where the * operator is the dereference operator . 其中*运算符是取消引用运算符 It dereferences pointers, so in this case *p3 etc are of type int . 它取消引用指针,因此在这种情况下*p3等的类型为int You can think of it as the inverse of the & address-of operator. 您可以将其视为&地址运算符的逆。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM