1. 函数指针 void (*f)()
2. 函数返回指针 void *f()
3. const 指针. const int * or int const *. 即指针指向的值不能被修改, const 在 * 左边
4. 指向 const 的指针. int * const, 指针本身不能被修改
例题1:
一个指向整型数组的指针定义为
A. int(*ptr) [] B. int *ptr[] C. int *(ptr[]) D. int ptr[]
A 是指向整型数组的指针
B, C 等价, 是指向数组的指针, 数组的元素是 int*
例题2:
int main() {
int a[] = {1, 2, 3, 4, 5};
int *ptr = (int *)(&a + 1);
printf("%d %d", *(a+1), *(ptr-1));
}
返回的结果是 2, 5
数组名本身就是指针, 再加上 & 就变成了双指针, 这里的双指针就是指二维数组, 加上 1, 就是数组整体加一行, ptr 指向第六个元素
例题3:
float (**def) [10]; def 是一个二级指针, 它指向一个一维数组的指针, 数组元素都是 float
double* (*gh) [10]; gh 是一个指针, 它指向一个一维数组, 数组的元素都是 double*
double (*f[10])(); f 是一个函数数组,
int (*(*f)(int, int))(int); f是一个函数指针, 参数为两个 int 并返回一个函数指针, 该函数的指针接受一个 int 并返回一个 int
例题4:
int main() {
char* a[] = {"hello", "the", "world"};
char **pa = a;
pa ++;
cout << *pa << endl;
return 0;
}
输出是 the
char* a[] 等价于 char* (a[]), pa 是一个二维数组