【轉載】指針參數是如何傳遞內存的?
摘要: 如果函數的參數是一個指針,不要指望用該指針去申請動態內存。示例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 閱讀(1872) |
評論 (0) 編輯