|
发表于 2015-4-29 15:45:22
|
显示全部楼层
本帖最后由 myqicq 于 2015-4-29 18:25 编辑
@追忆lh @小甲鱼
这是我的理解方式,希望对你有用,中国式英文,见笑了....
- #include <stdio.h>
- #include <string.h>
- void GetMemory(char **p, int num){
- printf("the GetMemory p address=%X\n", p); //理解 **p 是一个什么东西,有什么作用
- printf("the malloc to run befor *p the value is=%s\n", *p); //理解 *p 又是一个什么东西,其作用是什么
- *p = (char *)malloc(num); //申请num所指定大小的内存空间
- printf("the malloc to run after *p the value is=%s\n", *p); //为什么显示乱码,自己想一下,甲鱼哥常说的
- }
- void Test(void){
- char *str = NULL; //申明一个空指针变量str
- printf("the call GetMemory befor str the value is=%s\n", str);
- printf("the Test str address=%X\n", &str);
- GetMemory(&str, 100); //传str的地址给getmemory函数,在getmemory函数为str申请一块指定长度的内存空间
- printf("the call GetMemory after str the value is=%s\n", str); //为什么显示乱码,自己想一下,甲鱼哥常说的
- strcpy(str, "hello"); //把字符串"hello"复制到str所指向的空间
- printf("the strcpy to run after str the value is=%s\n", str);
- printf("%s\n", str); //打印str所指向的空间里的内容
- }
- void main(void)
- {
- Test();
- system("pause");
- }
复制代码 |
|