关于指针的辨析——指针,数组,数组指针,指针数组,函数指针,返回指针的函数

指针和数组的区别与联系:
数组名可以看作是指针常量

指针数组
函数指针 void (*f)(int *)
Attention: void *f(int *) declares f is a function
Because the order of operators
(void *) f(int *)

当遇见一个无比复杂的定义时,只要看括号最里面是什么即可,更准确地说,是看*最先运算的是什么操作
这就又涉及到操作符的运算顺序了,
以及操作符的作用对象:

Operators Associativity
() [] -> . ++ -- Left to Right
! ~ ++ -- + - * & (type) sizeof Right to Left
* / % Left to Right
+ - Left to Right
<< >> Left to Right
< <= > >= Left to Right
== != Left to Right
& Left to Right
^ Left to Right
` `
&& Left to Right
`
?: Right to Left
= += -= *= /= %= &= ^= != <<= >>= Right to Left
, Left to Right

举个例子:

定义 解释
char **argv; 定义一个指向指针的指针,可以用来接收命令行参数。
int (*daytab)[13]; 定义一个指向包含13个整数的数组的指针,daytab指向该数组。
int (*comp)(); 定义一个指向返回整数函数的指针,comp指向该函数。
char (*(*x())[])(); 定义一个指向返回指向字符数组的指针的函数的指针,x指向该函数。此语句是函数指针的定义,它是一个指向返回字符数组的函数的指针。
char (*(*x[3])())[5]; 定义一个数组,数组的每个元素是指向函数的指针,该函数返回一个包含5个字符的数组。x是该数组的名称。此语句是函数指针数组的定义。
C Language Statement Explanation
char **argv; Pointer to a pointer to char, typically used for command line arguments.
int (*daytab)[13]; Pointer to an array of integers with 13 elements, often used for representing a calendar.
int (*comp)(); Pointer to a function that takes no arguments and returns an integer.
char (*(*x())[])(); Function x returning a pointer to an array[] of pointers to functions returning char.
char (*(*x[3])())[5]; Array[3] of pointer to function returning pointer to array[5] of char

你可能感兴趣的:(《深入了解计算机系统》,笔记,c++)