2

I have read this question and I want to add to it that what are the things that can not be done using the comma operator. This has confused me a lot, as I can do this:

int arr[3];
arr[0]=1,arr[1]=2,arr[2]=3;

But when I do:

int arr[3],arr[0]=1,arr[1]=2,arr[2]=3;

It gives me a compiler error.

I want to ask that what are the limitations of the comma operator in real practice?

Community
  • 1
  • 1
  • Also an interesting question is of what *can* be done with the comma operator. Overloading the comma operator is the right tool to test at compile time whether an expression type is void or not, ie. `sizeof((expr, foo())) == sizeof(foo)` where `foo` is a user defined type, and there is a `template foo (&operator,(T, foo))[2];`. When used with "T = void", the default comma operator is used. – Alexandre C. Dec 28 '10 at 16:19

2 Answers2

10

One thing to realize is that not all uses of a comma in C are instances of the comma operator. Changing your second example to be a syntactically declaration:

int a0=1,a1=2,a2=3;

the commas are not operators, they're just syntax required to separate instances of declarators in a list.

Also, the comma used in parameter/argument lists is not the comma operator.

In my opinion the use of the comma operator is almost always a bad idea - it just causes needless confusion. In most cases, what's done using a comma operator can be more clearly done using separate statements.

Two exceptions that come to mind easily are inside the control clauses of a for statement, and in macros that absolutely need to cram more than one 'thing' into a single expression, and even this should only be done when there's no other reasonable option).

Michael Burr
  • 333,147
  • 50
  • 533
  • 760
  • Even if they are just separators here,still it should declare an array of 3 digits and assign the values from left to right according to me –  Dec 28 '10 at 16:04
  • 4
    @fahad: in some instances the comma is not an operator, the page you linked to also says, "Commas can be used as separators in some contexts, such as function argument lists. Do not confuse the use of the comma as a separator with its use as an operator; the two uses are completely different" – Michael Burr Dec 28 '10 at 16:07
  • Oh sorry I was changing my comment you caught me in between : ) –  Dec 28 '10 at 16:08
  • I corrected my answer - the problem is that what you were trying to do in your second example is not syntactically correct in a declaration (which is the error you saw). The comma used sometimes in declarations is not the 'comma operator'. – Michael Burr Dec 28 '10 at 16:10
  • 1
    `int arr[3],arr[0]=1,arr[1]=2,arr[2]=3;` is syntactically correct in that it follows the grammar rules. It tries to declare the same array multiple times (which is invalid), once with an illegal size, and with different invalid initializers. It's semantically very wrong though. – CB Bailey Dec 28 '10 at 16:15
  • I would like to add one more use of comma operator i.e to shorten the code into one line. I often come across such questions in SO :) –  Dec 28 '10 at 16:16
  • @fahad: That doesn't make any sense. You can shorten code onto one line by using a space instead of a newline. Both are equivalent ways to separate tokens in C++. Post pre-processing, newlines are not particularly special in C++. – CB Bailey Dec 28 '10 at 16:20
  • I meant using it instead of statement terminators when dealing with multiple expressions .Like http://stackoverflow.com/questions/4534734/want-to-add-an-integer-in-all-the-indexes-of-the-integer-array/4547134#4547134 –  Dec 28 '10 at 16:22
  • @fahad: There's no comma operator in `int arr[3],arr[0]=1,arr[1]=2,arr[2]=3;`, read @Michael Burr's answer again. That's no valid grammar reading that would make the comma part of an expression. Grammatically, it's a declaration of multiple entities all of which have the same name `arr`. It's invalid, but not because it isn't following the grammar. – CB Bailey Dec 28 '10 at 16:30
  • @fahad: `;` is a perfectly good statement terminator even when it doesn't appear at the end of a line. `,` isn't technically a statement terminator even if it makes a single expression statement act the same way as sequence of multiple statements. – CB Bailey Dec 28 '10 at 16:32
5

You can use the comma operator most anywhere that an expression can appear. There are a few exceptions; notably, you cannot use the comma operator in a constant expression.

You also have to be careful when using the comma operator where the comma is also used as a separator, for example, when calling functions you must use parentheses to group the comma expression:

void f(int, bool);

f(42, 32, true);   // wrong
f((42, 32), true); // right (if such a thing can be considered "right")

Your example is a declaration:

int arr[3],arr[0]=1,arr[1]=2,arr[2]=3;

In a declaration, you can declare multiple things by separating them with the comma, so here too the comma is used as a separator. Also, you can't just tack on an expression to the end of a declaration like this. (Note that you can get the desired result by using int arr[3] = { 1, 2, 3 };).

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • Probably worth pointing at that while the comma operator can be used where an _expression_ is expected, in many cases where people think of using expression often only an _assignment-expression_ is allowed. E.g. function arguments, initializers. – CB Bailey Dec 28 '10 at 16:24