user-0 提问: c指针问题

回复:
user: user-1
content: 自己写代码搞不清楚算符优先级请多加括号。 看别人代码搞不清楚算符优先级能调试的话请单步调试对应汇编。 看别人代码搞不清楚算符优先级不能调试的话想办法照写一小段孤立的可调试的代码然后单步调试对应汇编。 看别人代码搞不清楚算符优先级不能调试的话且没有办法照写一小段孤立的可调试的代码然后单步调试对应汇编的话只能参考算符优先级表猜了（提醒：并不能100%猜对）。 //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 | //+------------------+-----------------------------------------+---------------+

user: user-2
content: 指针数组：     整数指针（指向整数的指针）的数组：       int *a[2];         // 分解动作：            a [2]  =====> a 是个一维数组，元素共2个            *a[2]  =====> 再与*结合，说明，元素类型是指针         int *a[2] =====> 再连接上“前置类型”，说明指针指向整数。  指针数组：       指向整数数组的指针：         int (*a) [2];          // 分解动作：              *a  =====> 说明a是一个一级指针             （*a）[2] ====> 说明指针指向一个数组，数组元素共2个          int （*a)[2] ====> 结合前置类型，指出此数组元素的类型是int

user: user-3
content: 指针数组和数组指针应该分别怎么定义啊？ 引用 4 楼 sinservice 的回复:加括号是为了改变默认结合关系，  *a []  a [] 先结合， 然后跟*结合。 就是说， *a[] =等价于= * (a[]) ，  但不等价于  (*a) [] ，在最后一种情况里，强制指定 *a 先结合，表示 a 是一个指针，指向 sometype的一维数组。  int * 不能结合，就是说， 你不能写 (int *) a[] ， 这样写显然就不是变量定义，而是强制类型转换了，C没有这种写法。  int * 为什么不能结合呢？   因为C的一个类型表示是两部分组成，一部分是前置类型，一个是对id的声明。  举例： 一个C变量声明语句为 int **i, (* f)();  我们如果标出冗余的括号就是：  int (**i), ((*f)());   并等价于 int **i; int (*f)();   前置类型，只能是typename，不能包括* [] ()等等。  当然有的编译器存在简单写法，如VC:  (int (*)()) a  将a强制转换为 “指向 返回整数的函数 的指针”，在这里，其实是省略了写一个id，依然不能将这个类型视作前置类型。

user: user-2
content: 还要注意一点，就是const和volatile的结合问题。  const int 等价于 int const const int 等价于 int const *  前置类型可以向前结合N个const描述符，遇到 * id ( [ 就结束。

user: user-2
content: 加括号是为了改变默认结合关系，  *a []  a [] 先结合， 然后跟*结合。 就是说， *a[] =等价于= * (a[]) ，  但不等价于  (*a) [] ，在最后一种情况里，强制指定 *a 先结合，表示 a 是一个指针，指向 sometype的一维数组。  int * 不能结合，就是说， 你不能写 (int *) a[] ， 这样写显然就不是变量定义，而是强制类型转换了，C没有这种写法。  int * 为什么不能结合呢？   因为C的一个类型表示是两部分组成，一部分是前置类型，一个是对id的声明。  举例： 一个C变量声明语句为 int **i, (* f)();  我们如果标出冗余的括号就是：  int (**i), ((*f)());   并等价于 int **i; int (*f)();   前置类型，只能是typename，不能包括* [] ()等等。  当然有的编译器存在简单写法，如VC:  (int (*)()) a  将a强制转换为 “指向 返回整数的函数 的指针”，在这里，其实是省略了写一个id，依然不能将这个类型视作前置类型。

user: user-2
content: 没有任何区别。

user: user-4

user: user-5
content: 请看C语言基础