Jump to content
  • Advertisement
Sign in to follow this  
aeroz

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

This topic is 3613 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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();


Share this post


Link to post
Share on other sites
So
a[0,2] = 1;
has absolutely no sense (same as a[2] = 1;)

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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. :-)

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!