谈一些浅显的理解,如有错误欢迎指正
只有在强制转换类型时,才是变量类型结合后面的*,例如
a=(int*)malloc(5*sizeof(int));
在其他情况下都是后面变量结合前面的*,如声明指针变量时
int *p;
int (*p);//正确等价
(int*) p;//错误示范!!
在声明函数时可以不写形参,可以理解为隐藏了而并非没有,形参如果有指针理解为*与未写的参数结合
int test(int *);
int test(int *p);
int test(int (*));
int test(int (*p));//以上声明方式皆正确等价
int test((int*)p);
int test((int*));//错误示范!!
对于typedef定义指针类型时*同样要放到类型前面,如
typedef struct Lnode
{
data;
struct Lnode *next;
}*linklist;
当指针、数组、函数相结合时,先结合什么就是什么,如
int *p[4];
int *(p[4]);
以上由于[]优先级比*高,二者等价,因为先结合[]所以p是个数组!!!然后每个数组的元素时int型指针
int (*p)[4];
以上先结合*,所以p是个指针,指向4元素int类型数组
对于指针与函数,如
int *test(int);
int (*test(int)); //以上表示返回值是int型指针的函数
int (*test)(int);
int ((*test)(int));//指向函数的指针
(int*) (test(int));//错误示范!!!
前两个当中test先和后面的(int)结合,所以test本身是函数,然后和前面结合,返回值为int型指针
第三、四个test先和*结合,所以test是指针,再和后面结合表示函数,返回值为int型
最后一个是错误示范