二级指针
理解
- 指针变量也是变量,是变量就有地址
- 二级指针变量存放一级指针变量的地址
写法
int **p;
代码中理解
问题:有a个学生,每个学生有b门课程的成绩。要求在用户输入学生序号以后,能输出该学生的全部成绩。
分析思路在注释
#include <stdio.h>
void findNumber(int (*psud)[4], int pos, int **ppos) //这里如果是*ppos那么解引用操作 *ppos 会返回 ppos 所指向的地址处存储的整数值,其类型为 int
{
*ppos = (int *)(psud+pos); //这里需要的是int*类型,也就是对应数组的地址
}
int main() {
int student[3][4] = { {11, 21, 31, 44}, {54, 65, 77, 87}, {69, 10, 11, 12} };
int *ppos;
int pos;
// 提示输入学生序号
printf("输入学号,返回相应的成绩: ");
scanf("%d", &pos);
findNumber(student, pos, &ppos);
for (int i = 0; i < 4; i++) {
printf("%d ", *ppos++);
}
return 0;
}
执行结果