废话不多说,直接上示例:
//之所以定义二级指针**P,不是为了使用**P表示指向的值,而是为了使用*P来存储或者改变目标指针所指向的地址
void GetMemory2(char **p, int num)
{
*p = (char *)malloc(num); //二级指针p改变入参p指针指向的地址
}
void main(void)
{
char *str = NULL;
GetMemory2(&str, 100);//str指针指向的地址,以形参的方式传递给GetMemory2
strcpy(str, "hello\r\n");
printf(str);
free(str);
char *p = NULL;
char **p2 = NULL;
p2 = &p;
*p2 = (char* )0xF5; //改变p指针指向的地址 *p2存储的是p的值
printf("p-> %x\r\n",p);
unsigned char buff[10];
unsigned char *pbuff = buff;
*(pbuff+9) = 20;//一级指针可以改变数组的值
printf("pbuff+9 = %d\r\n",*(pbuff+9));
printf("buff[9] = %d\r\n",buff[9]);
}
//运行结果
hello
p-> f5
pbuff+9 = 20
buff[9] = 20