[問題] 困擾許久的struct 指標問題

看板C_and_CPP作者 (好好)時間9年前 (2014/10/17 04:31), 編輯推噓5(506)
留言11則, 6人參與, 最新討論串1/1
開發平台(Platform): (Ex: VC++, GCC, Linux, ...) C 問題(Question): 我知道 int a, *b; ---> 代表 b 為一個指向 int 的 ptr b = &a; ---> 代表 b 儲存 a 的 address 然而,換成struct時,我的困惑就來了 struct list { int data struct list *next }; struct list *b; ----> 代表 b 為一個指向 struct list 的 ptr ======== 我的問題如下: ============================= (1) b = (struct list*)malloc(sizeof(struct list)) --> 這行是否代表 b 儲存 一個新分配結構的 address? (2) 如果是,為何不寫成如下語法 b = & malloc(sizeof(struct list)) 畢竟 (struct list*) q 好像代表令 q 為指向struct list 的 ptr thanks !!! -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 120.126.10.139 ※ 文章網址: http://www.ptt.cc/bbs/C_and_CPP/M.1413491468.A.117.html

10/17 04:33, , 1F
type casting vs address of...
10/17 04:33, 1F

10/17 04:46, , 2F
抱歉 可否說明更詳細呢 謝謝
10/17 04:46, 2F

10/17 04:49, , 3F
malloc returns a pointer to allocated memory
10/17 04:49, 3F

10/17 04:50, , 4F
cast void* to struct list* vs address of void*
10/17 04:50, 4F

10/17 05:17, , 5F
thanx, your explaination is clear:)
10/17 05:17, 5F

10/17 07:51, , 6F
malloc是函數,重點在他的回傳值是數值還是位置
10/17 07:51, 6F

10/17 19:52, , 7F
(1) 是否應該為 malloc(sizeof(struct list*))
10/17 19:52, 7F

10/17 20:27, , 8F
樓上那樣只會拿到約 4-byte 的大小, 一存取就死翹翹
10/17 20:27, 8F

10/17 20:55, , 9F
喔對,我眼殘 別理我 XD
10/17 20:55, 9F

10/17 21:00, , 10F
我昨天只是 gentoo 裝 fcitx 和 rime 太慢才打英文XD
10/17 21:00, 10F

10/18 00:42, , 11F
XDD
10/18 00:42, 11F
文章代碼(AID): #1KG2iC4N (C_and_CPP)