核心
*X:表示指向某个变量的指针。(保存着 变量的地址)
**X:表示指向指针的指针。(保存着 指针的地址)
指针也是一种变量,用于存储地址。不要过度纠结指针指向的问题,将指针当做变量进行理解。
现象
解释
#include <stdio.h>
void test1 (int *tp);
void test2 (int **tp);
int main() {
int a = 100;
int *tp = &a;
printf("=============== main ===============\n");
printf("a 的地址为:%x\n", &a);
printf("tp 的地址为:%x\n", &tp);
test1(tp); // tp = a 的地址
test2(&tp); // &tp = tp 的地址
return 0;
}
void test1 (int *fp) { // fp = a 的地址
printf("=============== test1 ===============\n");
printf("tp = %x\n", fp);
printf("*tp = %d\n", *fp); // 输出 fp 中的地址对应空间,即 a 的地址空间中的值
}
void test2 (int **fp) { // fp = tp 的地址
printf("=============== test1 ===============\n");
printf("tp = %x\n", fp);
printf("*tp = %x\n", *fp); // 由于 fp = tp 的地址,所以 *fp == tp,输出 tp 中保存的地址,即 a 的地址
printf("**tp = %d\n", **fp); // 根据上文 *fp = tp,所以 **fp = *tp,输出 tp 中的地址对应空间,即 a 的地址空间中的值
}