Reputation: 1244
Lets say I have the following code,
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.
};
After this, I've got some functions that initialize these structs. To prevent too much code in this post, I'm going to avoid posting those functions here.
Now, lets say I have the following,
WordNode **p = wset->head; // can't change this line
Here, p is basically a pointer pointing to a pointer, correct?
And then, if I do this:
(*p) == NULL
This would return true if p is pointing to NULL, right?
Now, how would I get the word stored in wset->head?
Can I do this?
(*(*p))->word
And if I want p to point to the next WordNode, can I do this?
p = (*(*p))->next
I just want to know if all this is valid syntax so that I know I'm using pointers correctly.
Upvotes: 0
Views: 70
Reputation: 305
Just for the sake of simplicity, whenever I have to deal with double pointer and I need the value I all the time go via an intermediate variable
e.g.
WordNode **ptr2ptr = wset->head;
WordNode *ptr = *ptr2Ptr;
WordNode value = *ptr;
or to get the head:
WordNode head = ptr->head;
Now I'm just answering the question which is, how to access the value of a pointer to pointer. You must be careful that your wset->head contains actually a pointer to pointer which is normally not the case in a linked list the way I understand you are trying to do. This is also not the way you have defined your head member...
Upvotes: 0
Reputation: 6783
Not really. (*(*p))->word
is a total dereferenciation. So it would either be (*(*p)).word
or (*p)->word
.
You can imagine it that way, that the ->
-Operator takes away one reference for you.
obj->field
is the same as (*obj).field
Upvotes: 1