c,c++,运算符优先级

3* ( *p)++ 为什么先算3 * ( *p)而不先算自增?++的优先级不是优先于乘法吗

++写在前面会先进行自增,再按优先级顺序进行运算。
++写在后面会先按优先级顺序完成除了自增以外的所有运算,最后进行自增。

( *p)++ 是先计算求值,但是返回的是*p在自增之前的值。
3* (++(*p)),这才是返回自增之后的值

自增运算符写在后面都是当前语句结束后在执行


//C++ Operators
//  Operators specify an evaluation to be performed on one of the following:
//    One operand (unary operator)
//    Two operands (binary operator)
//    Three operands (ternary operator)
//  The C++ language includes all C operators and adds several new operators.
//  Table 1.1 lists the operators available in Microsoft C++.
//  Operators follow a strict precedence which defines the evaluation order of
//expressions containing these operators.  Operators associate with either the
//expression on their left or the expression on their right;    this is called
//“associativity.” Operators in the same group have equal precedence and are
//evaluated left to right in an expression unless explicitly forced by a pair of
//parentheses, ( ).
//  Table 1.1 shows the precedence and associativity of C++ operators
//  (from highest to lowest precedence).
//
//Table 1.1   C++ Operator Precedence and Associativity
// The highest precedence level is at the top of the table.
//+------------------+-----------------------------------------+---------------+
//| Operator         | Name or Meaning                         | Associativity |
//+------------------+-----------------------------------------+---------------+
//| ::               | Scope resolution                        | None          |
//| ::               | Global                                  | None          |
//| [ ]              | Array subscript                         | Left to right |
//| ( )              | Function call                           | Left to right |
//| ( )              | Conversion                              | None          |
//| .                | Member selection (object)               | Left to right |
//| ->               | Member selection (pointer)              | Left to right |
//| ++               | Postfix increment                       | None          |
//| --               | Postfix decrement                       | None          |
//| new              | Allocate object                         | None          |
//| delete           | Deallocate object                       | None          |
//| delete[ ]        | Deallocate object                       | None          |
//| ++               | Prefix increment                        | None          |
//| --               | Prefix decrement                        | None          |
//| *                | Dereference                             | None          |
//| &                | Address-of                              | None          |
//| +                | Unary plus                              | None          |
//| -                | Arithmetic negation (unary)             | None          |
//| !                | Logical NOT                             | None          |
//| ~                | Bitwise complement                      | None          |
//| sizeof           | Size of object                          | None          |
//| sizeof ( )       | Size of type                            | None          |
//| typeid( )        | type name                               | None          |
//| (type)           | Type cast (conversion)                  | Right to left |
//| const_cast       | Type cast (conversion)                  | None          |
//| dynamic_cast     | Type cast (conversion)                  | None          |
//| reinterpret_cast | Type cast (conversion)                  | None          |
//| static_cast      | Type cast (conversion)                  | None          |
//| .*               | Apply pointer to class member (objects) | Left to right |
//| ->*              | Dereference pointer to class member     | Left to right |
//| *                | Multiplication                          | Left to right |
//| /                | Division                                | Left to right |
//| %                | Remainder (modulus)                     | Left to right |
//| +                | Addition                                | Left to right |
//| -                | Subtraction                             | Left to right |
//| <<               | Left shift                              | Left to right |
//| >>               | Right shift                             | Left to right |
//| <                | Less than                               | Left to right |
//| >                | Greater than                            | Left to right |
//| <=               | Less than or equal to                   | Left to right |
//| >=               | Greater than or equal to                | Left to right |
//| ==               | Equality                                | Left to right |
//| !=               | Inequality                              | Left to right |
//| &                | Bitwise AND                             | Left to right |
//| ^                | Bitwise exclusive OR                    | Left to right |
//| |                | Bitwise OR                              | Left to right |
//| &&               | Logical AND                             | Left to right |
//| ||               | Logical OR                              | Left to right |
//| e1?e2:e3         | Conditional                             | Right to left |
//| =                | Assignment                              | Right to left |
//| *=               | Multiplication assignment               | Right to left |
//| /=               | Division assignment                     | Right to left |
//| %=               | Modulus assignment                      | Right to left |
//| +=               | Addition assignment                     | Right to left |
//| -=               | Subtraction assignment                  | Right to left |
//| <<=              | Left-shift assignment                   | Right to left |
//| >>=              | Right-shift assignment                  | Right to left |
//| &=               | Bitwise AND assignment                  | Right to left |
//| |=               | Bitwise inclusive OR assignment         | Right to left |
//| ^=               | Bitwise exclusive OR assignment         | Right to left |
//| ,                | Comma                                   | Left to right |
//+------------------+-----------------------------------------+---------------+

【以下回答由 GPT 生成】

这个问题是由于表达式的解析顺序引起的。表达式的解析顺序由运算符优先级和结合性决定。

在C/C++中,一元运算符(如自增操作符++)的优先级是最高的,而二元运算符(如乘法操作符*)的优先级较低。然而,表达式的解析顺序不仅仅取决于运算符的优先级,还受到括号的影响。

在这个特定的表达式3(p)中,由于括号的存在,解析器会首先计算*p,然后再将结果与3相乘。这是因为括号具有更高的优先级,它们会改变运算符的解析顺序。

为了更好地理解,让我们来看一个简化的例子:

int x = 2;
int* p = &x;
int result = 3 * (*p);

在这个例子中,我们首先声明一个变量x并将其初始化为2。然后,我们声明一个指针变量p并将其指向x。最后,我们计算表达式3 * (*p)并将结果保存在result变量中。

在计算表达式3 * (*p)时,解析器首先执行括号内的操作,即解引用指针p并获取其指向的值x的内容。然后,它将此值2乘以3,得到最终的结果6。

因此,表达式3(p)中的自增操作符并没有被计算,因为它在这个表达式中并不存在。如果你想使用自增操作符,可以按照以下方式修改代码:

int x = 2;
int* p = &x;
++(*p);  // 先执行自增操作
int result = 3 * (*p);  // 再执行乘法运算

在这个修改后的版本中,我们先执行自增操作符++来递增指针p指向的值x。然后,我们再计算乘法表达式3 * (*p)并将结果保存在result变量中。

总结起来,表达式的解析顺序取决于运算符的优先级和结合性,并受到括号的影响。在处理表达式时,要注意运算符的优先级和结合性以及括号的使用,以确保表达式得到正确的求值顺序。



【相关推荐】



如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^