0

I have two structs as such, and I'm trying to fully grasp the usage of the arrow operator. Where Item is a struct, which contains a name and grade.

typedef struct ListNodeTag {
    Item item;
    struct ListNodeTag *next;
} ListNode;

typedef struct {
    int size;
    ListNode *first;
} List;

I understand that if I do something like:

List * L;
L = malloc(sizeof(List));

L->first;  // this refers to the first element

But what if I had something like L->first->first?

Or

ListNode * p,q;
 p->next = q;

Or p->next->next; ?

I get that the arrow notation is deference's and and access member values/variables. But I'm not sure what the last 3 things I've stated are doing exactly? Any help would be much appreciated.

The three examples here:

L->first->first
p->next = q
p->next->next

etc

  • 2
    `L->first->first` won't compile, because `ListNode` has no `first` item. You could do `L->first->next`, though. – Cornstalks Nov 02 '15 at 03:47
  • It's unclear from your title and question whether your confusion is with the arrow operator itself, or with the multiple uses of it (the latter has more to do with linked lists than with the arrow operator). –  Nov 02 '15 at 03:51
  • @Cornstalks Yeah ,I missed that . – ameyCU Nov 02 '15 at 03:52
  • Grab a piece of paper and draw boxes and arrows. That may help clear things up. You can write contents inside the boxes, and (just below or above the box) give them an address as well. –  Nov 02 '15 at 03:52
  • More so with the multiple uses of it, as in if p,q,r are 3 elements in that order of a linked list. Does p->next point to q, and then p->next->next point to r? It's also a matter of confusion of whether L->first points to the first element of the list, if so If I could do something like L->first->next to set the first elements next pointer to something.. – user5514313 Nov 02 '15 at 03:53
  • [You might find this description useful.](https://gist.github.com/DaoWen/2bf3b1cce88aae0bf958) – DaoWen Nov 02 '15 at 03:59
  • Thanks, that really helped. – user5514313 Nov 02 '15 at 04:30

0 Answers0