[英]Pointer to a pointer, how do I get values?
可以說我有以下代碼,
typedef struct WordNode WordNode;
struct WordNode {
int freq; // The frequency of the the word.
char *word; // The word itself.
WordNode *next; // The next word node in the list.
};
struct WordSet {
int size; // The number of elements in the set.
WordNode *head; // The starting node in the set.
};
在此之后,我得到了一些初始化這些結構的函數。 為了防止在這篇文章中編寫過多的代碼,我將避免在此處發布這些功能。
現在,假設我有以下情況,
WordNode **p = wset->head; // can't change this line
在這里,p基本上是指向指針的指針,對嗎?
然后,如果我這樣做:
(*p) == NULL
如果p指向NULL,則返回true,對嗎?
現在,如何將單詞存儲在wset-> head中?
我可以這樣做嗎?
(*(*p))->word
如果我想讓p指向下一個WordNode,可以這樣做嗎?
p = (*(*p))->next
我只想知道所有這些是否都是有效的語法,以便知道我正確使用了指針。
並不是的。 (*(*p))->word
是總取消引號。 因此它可能是(*(*p)).word
或(*p)->word
。
您可以這樣想象->
-Operator為您刪除一個參考。
obj->field
與(*obj).field
只是為了簡單起見,每當我必須處理雙指針並且需要隨時通過中間變量訪問值時,
例如
WordNode **ptr2ptr = wset->head;
WordNode *ptr = *ptr2Ptr;
WordNode value = *ptr;
或得到頭:
WordNode head = ptr->head;
現在,我只是在回答一個問題,即如何訪問指向指針的指針的值。 您必須小心wset-> head實際上包含一個指向該指針的指針,在鏈表中,通常不是我所理解的那樣。 這也不是您定義負責人的方式...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.