Adam D. Ruppe wrote:
On Mon, Nov 16, 2009 at 10:12:57PM +0000, dsimcha wrote:
Can someone please explain to me what the comma operator does?

        a = b, c;

Is the same as:

        b;
        a = c;

In the comma operation, the first thing is evaluate, and its result discarded.
Then, the second thing (after the comma) is evaluated. The overall result
of the comma operator is the result of the second statement.

        1, 2 == 2

The usefulness is putting two statements together, where it only expects
one, like inside the for loop, where the semicolon moves you on from the
initialization to the condition.

Speaking of for loops, it's not uncommon to use the comma operator in the 1st and 3rd expression places, e.g. (in C), something along the lines:

void fun( char* start, int n) {
  int i;
  char* p;

  for ( i = 0, p = start; (i < n) && (*p != 0); ++i, ++p) {
    /* ... */
  }

}

Reply via email to