Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of new int[25,2]?

Tags:

c++

What is the meaning of using the second parameter with a comma in the below code?

int *num = new int[25,2];
like image 865
Jay Avatar asked Jun 24 '10 06:06

Jay


3 Answers

That's the comma operator in action: it evaluates it's operand and returns the last one, in your case 2. So that is equivalent with:

int *num = new int[2];

It's probably safe to say that the 25,2 part was not what was intended, unless it's a trick question.

Edit: thank you Didier Trosset.

like image 123
Eugen Constantin Dinca Avatar answered Oct 17 '22 15:10

Eugen Constantin Dinca


That's the comma operator in action: it evaluates it's operand and returns the last one, in your case 2. So that is equivalent with:

int *num = new int[2];
like image 26
Didier Trosset Avatar answered Oct 17 '22 15:10

Didier Trosset


You are using the comma operator, which is making the code do something that you might not expect at a first glance.

The comma operator evaluates the LHS operand then evaluates and returns the RHS operand. So in the case of 25, 2 it will evaluate 25 (doing nothing) then evaluate and return 2, so that line of code is equivalent to:

int *num = new int[2];
like image 10
Peter Alexander Avatar answered Oct 17 '22 15:10

Peter Alexander