【轉(zhuǎn)載】《Writing clean code》讀書筆記
摘要: 《Writing clean code》讀書筆記
閱讀全文
posted @
2011-05-29 20:37 deercoder 閱讀(2939) |
評論 (2) 編輯
【轉(zhuǎn)載】指針參數(shù)是如何傳遞內(nèi)存的?
摘要: 如果函數(shù)的參數(shù)是一個指針,不要指望用該指針去申請動態(tài)內(nèi)存。示例7-4-1中,Test函數(shù)的語句GetMemory(str, 200)并沒有使str獲得期望的內(nèi)存,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"); // 運(yùn)行錯誤
}
閱讀全文
posted @
2011-05-28 12:19 deercoder 閱讀(1881) |
評論 (0) 編輯