【转载】指针参数是如何传递内存的?
				     摘要:  如果函数的参数是一个指针,不要指望用该指针去申请动态内存。示例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 阅读(1893) | 
评论 (0)  编辑