What is the difference between these two lines of C code?
node = node->next; *node = *(node->next); node is a linked list Node struct which is defined as following:
struct Node{ int data; Node* next; }; Node* node; The first code snippet, node = node->next;, is a pointer assignment. That is, the address value that is currently in node will be replaced with the address that is in node->next.
The second snippet, *node = *(node->next);, dereferences the pointers, and copies the actual data of the structure that the RHS points to into the structure that the LHS points to. This will be equivalent to the following:
node->data = node->next->data; node->next = node->next->next; Note: In this second case, the address that node contains does not change; that is, it still points to the same location in memory, but the contents of that memory will be changed. In the first case, node will point to a different (probably) memory location, and the contents of the old location will be left untouched.
nodeis pointer toNodestructure, i.e.Node * node;