随笔-145  评论-173  文章-70  trackbacks-0
  2011年5月28日
     摘要: 如果函数的参数是一个指针,不要指望用该指针去申请动态内存。示例7-4-1中,Test函数的语句GetMemory(str, 200)并没有使str获得期望的内存,str依旧是NULL,为什么?

void GetMemory(char *p, int num)
{
p = (char *)malloc(sizeof(char) * num);
}
void Test(void)
{
char *str = NULL;
GetMemory(str, 100); // str 仍然为 NULL
strcpy(str, "hello"); // 运行错误
}
  阅读全文
posted @ 2011-05-28 12:19 deercoder 阅读(1842) | 评论 (0)编辑 收藏