----
不能传递, str还是0;
str值为1;
如下例子:
#include#i nclude void getmemory(char *p){p=(char *) malloc(100);strcpy(p,"hello world");}int main( ){char *str=NULL;getmemory(str);printf("%s/n",str);free(str);return 0;} 程序崩溃,getmemory中的malloc 不能返回动态内存, free()对str操作很危险
-形参 同级别,不能传递;
#include#include #include<string.h>void GetMemory1(char **p) {*p = (char *)malloc(100); }void main() {char *str = NULL;//GetMemory1(str);GetMemory1(&str);strcpy(str, "hello world");printf(str); }
这样可以;
这其实是一个参数传递的问题.
修改变量值需要传递该变量类型的一级指针;
修改一级指针指需要传递对应类型的二级指针.
----