0
Node<T> *ptr = Head;
while (ptr)
{
T n = ptr -> data;
ptr = ptr -> next;
}

ptr = current index pointer,
next = linked list pointers,
data = node data.

I understand that it's iterating through the pointers and values of the linked list. But what i don't understand, how these work:

ptr -> data;
ptr -> next;

if someone could step by step me through how these expressions get evaluated, that would be great.

edit::

enter image description here

how does:

head->next;

get evaluated to address 2200. i dont get how (*head).next can = 2200 when it's not being pointed to by head. not unless data and next share the same address? which im fairly sure is false.

  • possible duplicate of [Why does the arrow (->) operator in C exist?](http://stackoverflow.com/questions/13366083/why-does-the-arrow-operator-in-c-exist) – GorvGoyl Mar 25 '15 at 12:39
  • Short answer : someone is being evil (once more) : `ptr -> data` is equivalent to `ptr->data` is equivalent to `(*ptr).data`. Node is a templated struct. data is a member of that struct of type T. – Félix Cantournet Mar 25 '15 at 13:18
  • look the answer again, I add your last question edit. – Raydel Miranda Mar 25 '15 at 14:42

3 Answers3

1

A Node has to main members:

1 - A pointer to another node. (ptr, in your example)

2 - The data value it holds. (data, in your example)

Head stands for the first node in the list.

So,

Node<T> *ptr = Head;    // ptr points the ftrst node in the list.
while (ptr)             // the last node points nowhere (NULL), so if ptr is NULL we hit the end of the list.
{
T n = ptr -> data;      // This no need explanation.
ptr = ptr -> next;      // now we want to ptr point to the node is pointing its `next` pointer.
}  

This is how the pointer ptr advances through the list:

Describe how the pointer <code>ptr</code> advances through the list

Further questions

Why do we have to derefence the pointer to access the next pointer or even the data itself?

You don't have to.

ptr->data;  // data is a member in a struct/class POINTED by ptr.
*ptr;       // this is the class/struct INSTANCE (dereferenced). So
*ptr.data;  // data is a member of that instance.

If you have:

Node a; Node *ptr = &a; // Following are the same: a.next; ptr->next; *ptr.next;

Also since ptr is will point at data, and you dereference that to get the actual value of the data. is that correct?

No, ptr never will be pointed to data (according the example code).

ptr->data;  // This not means ptr will POINT to data.
            // this means "access to the data member of whatever object being POINTED by ptr".

Based on the code given, ptr will actually point at every single member in the linked list?

Yes, you got it!!

How does ptr->next get evaluated to the address of the next node?

A pointer is also a variable. An int variable hold int values, a pointer hold addresses.

A very simple (and useless) Node would be something like this:

struct Node {
    Node *next;   // Pointer to another node, yeah but ... which node? In a minute ;)
};

at some point you will have to write code like this:

// Example of adding a node.
Node* Head = new Node;
Node* another_node = new Node;
Head->next = another_node;  // This is how Head->next "knows" which node is the next. Because we told him.

So, now if we point at the same address of Head with another pointer, lets say ptr ...

Node *ptr = Head;

Then we have access to Head's next member through ptr->next. And ofcourse this evaluates to the address of another_node.

Raydel Miranda
  • 13,825
  • 3
  • 38
  • 60
  • ahhhh. I think i may have worded my question wrong, sorry. i meant, why do we have to deference the pointer to access the next pointer or even the data itself. and correct me if i'm wrong, but is that because the data and the next pointer are stored in a struct or a class? and in order to access it, we have to dereference our ptr pointer. but another question, based on the very first loop of the code i provided above, if we dereference *ptr will that return (on the first loop would return head) and on the next loops. will return next? –  Mar 25 '15 at 13:12
  • also since ptr is will point at data, and you dereference that to get the actual value of the data. is that correct? –  Mar 25 '15 at 13:13
  • so, based on the code given, ptr will actually point at every single member in the linked list? –  Mar 25 '15 at 13:14
  • @tropicalmemes I edited the answer to include your other questions. – Raydel Miranda Mar 25 '15 at 13:33
  • check my edit of my question though, that sums what i was trying to ask –  Mar 25 '15 at 14:10
  • Ahhh Kk I'm slowly understanding this. So you're accessing the node members/strict members. But can you do a step by step example of how ptr -> next gets evaluated the way I'm seeing it is like this once the expression is broken down;;;(;;;;; dereferenceValue.next ;how does it known which struct it's accessing? –  Mar 25 '15 at 15:06
  • ok, so i finally got the answer and i was just reading the illustrations from the examples incorrectly. nodePtr -> data just accesses the struct its self and i was looking at the expression like this 2100.data and i thought that 2100 was the address of the data member, but 2100 is actually the address of the instantiated struct, and when it's dereferenced it accesses the struct thats been instantiated. not sure if that's is correct or more or less, im not sure if thats exactly whats happening, but its given me enough understanding. but i the expression like this now. structNode.data = 12. –  Mar 26 '15 at 04:06
0

ptr -> data is equivalent to (*ptr).data

GorvGoyl
  • 42,508
  • 29
  • 229
  • 225
0

For ordinary pointers like Node<T> *ptr, the -> operator is equivalent to dereferencing the pointer and then accessing a member. Hence, your examples are equivalent to:

(*ptr).data;
(*ptr).next;

In other words, the expressions evaluate to the members of the object pointed to by ptr.

Rob Hague
  • 1,409
  • 9
  • 14