0

I am trying to understand how this bit of code below works.

while (c->next != NULL) c = c->next;
c->points[c->pointNum] = p;
c->pointNum++;

Are lines 2 and 3 here under the influence of the while loop? How does a one line while loop work?

hkj447
  • 677
  • 7
  • 21

1 Answers1

1

The code could be rewritten like this to be clearer:

// advance until c->next is NULL
while (c->next != NULL) {
    c = c->next;
}

// Now c is the last node in the chain
c->points[c->pointNum] = p;
c->pointNum++;

As you can see, your form takes advantage of the C language one-line support (blocks one line long need not be enclosed by braces). And as you experienced, this form, while more compact, can be more confusing.

LSerni
  • 55,617
  • 10
  • 65
  • 107