c语言 char **p
来源:百度知道 编辑:UC知道 时间:2024/05/17 01:18:36
请说明下char **p,这个不明白!
void GetMemory2(char **p, int num)
{
*p = (char *)malloc(sizeof(char) * num);
}
void Test2(void)
{
char *str = NULL;
GetMemory2(&str, 100);
strcpy(str, "hello");
cout<< str << endl;
free(str);
}
void GetMemory2(char **p, int num)
{
*p = (char *)malloc(sizeof(char) * num);
}
void Test2(void)
{
char *str = NULL;
GetMemory2(&str, 100);
strcpy(str, "hello");
cout<< str << endl;
free(str);
}
char型的双指针你可以理解为指向字符串的指针,使用双指针将分配的新的地址空间返回,你可以尝试单指针,看看效果就知道和本程序的意图并不相符。
原本char*用来做字符数组。函数里面需要指针才能把参数的值回传,所以用了指针的指针。可以看一下关于形参、实参这一类东西。