What does a[x,y] mean? (C++)

Started by
6 comments, last by DevFred 15 years, 8 months ago
Hello! I program in Cplusplus for quite a long time now. But today I saw that MS Visual Cplusplus accepts a syntax that I have never seen. What does this mean?
int a[5] = {0};
a[0,2] = 1; // what is done here?

// or maybe this?
if ( a[3,2] == 4 )
  DoSomething();
Thx! aeroz PS: why can't I write the plus sign in this forum?
Advertisement
Quote:Original post by aeroz
Hello!

I program in Cplusplus for quite a long time now. But today I saw that MS Visual Cplusplus accepts a syntax that I have never seen. What does this mean?

*** Source Snippet Removed ***

Thx!
aeroz

PS: why can't I write the plus sign in this forum?


First, you can write the plus sign, it just shows up incorrectly in preview. Ignore preview and post away.

Second, read this link explaining the comma operator, the existence of which explains that code snippet.

edit: additional information. You can overload the comma operator. Good information in this gamedev post.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

The comma operator evaluates its operands and returns the rightmost one. That code is equivalent to:
int a[5] = {0};a[2] = 1; // what is done here?// or maybe this?if ( a[2] == 4 )  DoSomething();
So
a[0,2] = 1;
has absolutely no sense (same as a[2] = 1;)

Thank you!
(and now I can write C++ [smile] )
Quote:Original post by aeroz
So
a[0,2] = 1;
has absolutely no sense (same as a[2] = 1;)

Thank you!
(and now I can write C++ [smile] )

Yeah this is usually a logic error or someone coming from another language like C# where this is how multi-dimensional arrays are declared.
[size="2"]Don't talk about writing games, don't write design docs, don't spend your time on web boards. Sit in your house write 20 games when you complete them you will either want to do it the rest of your life or not * Andre Lamothe
That would depend on the compiler you are using, but if you turned on warnings (so that the compiler would report things that are technically not syntax errors but most probably indicate a logical error on the programmer's part), you might see something like:

Quote:
MingW
[Warning] left-hand operand of comma has no effect

VC++
warning C4709: comma operator within array index expression

Comeau Online
"ComeauTest.c", line 4: warning: expression has no effect
a[0,2] = 1; // what is done here?
^


or similar.
Quote:Original post by aeroz
So
a[0,2] = 1;
has absolutely no sense (same as a[2] = 1;)

Thank you!
(and now I can write C++ [smile] )


Well, and this is purely academic, this would allow me to write a function like this:

pseudo code:
addEveryOtherElem( float array[], int size ){    while( size-2 > 0 )        sum += array[--i,--i];}


However, this is unintuitive and should never appear in professional code. It probably doesn't produce radically different assembly than if I decremented i twice in two separate statements THEN added the it to the sum.

The only time I ever use the comma operator is I have to do a primary and continuation read of something:

while( cin >> whatever, x > 9 ) // Assign whatever. Then, if it's greater than 9:

But, I'm told that's not the best of code and might get me in trouble. I usually don't really care, though, because I've never needed to do a primary and continuation read out side of school.
Quote:Original post by Splinter of Chaos
However, this is unintuitive and should never appear in professional code.
It's more usual than you would wish. A good number (luckily not all!) of C programmers who also know Perl write code like that. Heck, you cannot even blame them, since it's perfectly legal. I don't think they even mean to write obfuscated code, it's just they way they're used to do it.

My favourite snip is from Subottin's famous (and often copied) range coder:
while ((low^(low+range))<TOP || range<BOT && ((range=-low & (BOT-1)),1))

Sure enough, it's legal and it works just fine. When you think about it for a moment, then the intent becomes clear, too. However, when you glance at it for the first time, "WTF!?" is probably your first impression. :-)
Quote:Original post by Splinter of Chaos
pseudo code:

You mean --size instead of --i, right? But why would you see if size-2 is greater than zero (btw nonnegative would make more sense) and decrease size by two in two steps after that? Why not just decrease size by 2 immediately? I would write it this way:
float addEveryOtherElem(float array[], int size){    float sum = 0;    while((size-=2) >= 0)        sum += array[size];    return sum;}

This topic is closed to new replies.

Advertisement