在理解二级指针时,自己写了一个小程序帮助理解,
运行后,看到结果感觉明白了,很多。
中间红色部分,是用来帮助理解的,
程序会在这里死掉,
因为读取的,p_1中的地址内容为NULL,
读取NULL指向的内容肯定会出错。
但是这也帮助理解了,指针变量是怎么工作的:
首先,指针变量是一个变量
#include <stdio.h>
#include <string.h>
int test_1(int * point)
{
printf("point : %08x\n", point);
point = malloc(512);
printf("point : %08x\n", point);
return 0;
}
int test_2(int ** point)
{
printf("2_point: %08x\n", point);
*point = malloc(512);
printf("*point: %08x\n", *point);
return 0;
}
int test_3(int *** point)
{
printf("3_point:%08x\n", point);
*point = malloc(512);
printf("*3_point:%08x\n", *point);
return 0;
}
int main(int arg, char **argv)
{
int * p_1 = NULL;
int **p_2 = NULL;
int ***p_3= NULL;
printf("p_1:%08x \n", p_1);
printf("&p_1:%08x \n", &p_1);
printf("*p_1:%08x \n", *p_1);
printf("p_2:%08x \n", p_2);
printf("p_3:%08x \n", p_3);
printf("=========================\n");
test_1(p_1);
test_2(&p_1);
test_3(&p_2);
printf("p_1:%08x\n", p_1);
printf("p_2: %08x\n", p_2);
printf("p_3: %08x\n", p_3);
while(1);
}