看板 C_and_CPP 關於我們 聯絡資訊
int makeList(List_t* list) { list = malloc(sizeof(List_t)); if(!list){ return list_makeFail; }else{ return list_success; } } bool isEmpty(List* list) { return (list->head == NULL); } int main() { int ret; List_t* testList; ret = makeList(testList); printf("make list is %d\n", ret); ret = isEmpty(testList); printf("list is empty : %s\n", ret ? "true":"false"); return 0; } 測了一下是死在isEmpty() 另外試了一下: List_t testList = malloc(sizeof(List_t)); isEmpty(testList); 這樣是沒問題的,我是哪邊的觀念有錯誤嗎? 平台是centos7,用的是C -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 175.98.120.34 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1571225142.A.670.html
ab4daa: makeList裡面malloc的pointer沒傳出來 10/16 19:47
ab4daa: 所以isEmpty一用就爆 10/16 19:47
withoutshine: 沒看到 List_t 的宣告,但我猜你需要的是 10/16 20:56
withoutshine: a pointer to a pointer 10/16 20:56
loveme00835: 因為愛 10/17 00:57
Ryspon: 推三樓,另外可能也要複習一下 call by value 跟 pointer 10/17 14:19
dces4212: 兩種做法,不是用 ptr to ptr 就是回傳 malloc 拿到的 10/17 20:58
dces4212: 記憶體位置給 testList,你這實作比較適合用前者 10/17 20:58
Gway: pass pointer 變數到fun中會是一個副本(I.e copy)你的 mak 10/18 16:50
Gway: eList(List_t* list)中的list 不是你原本預期main裡面的test 10/18 16:50
Gway: list變數 10/18 16:50
elysium5290: 在malloc後/ main內打印一下address就知道問題在哪了 02/28 15:31