2

I have a question regarding the for loop in the release list func. (The head is a dummy so we don't need to release it). I don't understand the:

next = next?

part in the loop. What does it actually do, and why simply

next?

won't suffice? here is the code:

typedef struct NODE {
int num;
struct NODE* next;
} Node;




void release_list(Node* head) {
    Node* next = head->next ? head->next->next : NULL;
    for( Node* curr = head->next;
         curr != NULL;
         curr = next, next = next? next->next : NULL) {
        free(curr);
     }
}
Codor
  • 17,447
  • 9
  • 29
  • 56
Bar
  • 196
  • 10
  • It's the [conditional (a.k.a. ternary) operator](http://en.cppreference.com/w/c/language/operator_other#Conditional_operator). I know there's a good duplicate question, but I just can't seem to find it at the moment. – Some programmer dude Feb 08 '17 at 08:13

5 Answers5

3

The part :

curr = next, next = next? next->next : NULL

is the third part of the for loop, which means that it will be executed each time it ends. It is equivalent to :

curr = next;
if (next)                 //if next != NULL
    next = next->next;
else
    next = NULL;

What the code is trying to achieve is to check all pointers so as to avoid trying to access NULL->next on the last element, which will not exist.


In general, an if statement like the following :

if (a > b) {
    result = x;
} else {
    result = y;
}

can be rewritten as the following statement:

result = a > b ? x : y;

as shown in this link.

You can read more about the Ternary operator in C.

Marievi
  • 4,951
  • 1
  • 16
  • 33
3

Ternary operator ?: has higher precedence than assignment =.

Code is equivalent to:

next = (next? next->next : NULL)

Or:

if(next != NULL)
    next = next->next;
else
    next = NULL;

Point of the code is to avoid accidentally doing NULL->next on last element.

user694733
  • 15,208
  • 2
  • 42
  • 68
1

The for loop can be rewritten as a while loop, as follows:

Node* curr = head->next;
while(curr != NULL)
{
    free(curr);
    curr = next;
    if (next != NULL)
    {
        next = next->next;
    }
    else
    {
        next = NULL;
    }
}

The intention of next = next?next->next:NULL is to move the next pointer to its next node.

See: The ternary (conditional) operator in C

Community
  • 1
  • 1
ctrl-shift-esc
  • 876
  • 1
  • 9
  • 19
0

It's a part of the ?: operator. The construct is cond?consequent:alternative. If cond evaluates true the expression consequent is evaluated and it's value is the result of the expression, otherwise alternative is evaluated and it's value is the result of the expression. The type of consequent and alternative should be the same.

So next ? next->next : NULL means next->next if next is not NULL and NULL if next is NULL.

Additionally the , operator means to evaluate the expressions in sequence. This is the lowest prioritized operator which means that the last of the for clause means that first evaluate curr = next and then next = next ? next->next : NULL.

skyking
  • 13,817
  • 1
  • 35
  • 57
0

This is an example of obscuation and almost certainly the code is a failed attempt to delete all nodes in a linked list. A faster and more readable way to do the same would be:

void release_list (Node* head)
{
  while(head != NULL)
  {
    Node* tmp = head;
    head = head->next;
    free(tmp); 
  }
}
Lundin
  • 195,001
  • 40
  • 254
  • 396