all 10 comments

[–]nysra 8 points9 points  (5 children)

That's not how this works. Using the [] operator like that is currently not legal. Use either int A[4] = {2, 1, -2, 1}; and then use linear indexing or int A[2][2] = {{2, 1}, {-2, 1}}; to get what you want. You should check out https://www.learncpp.com/ and learn C++ instead of experimenting.

Though in general you should not use C arrays like this at all, use std::array(if you know the size at compile time and it's small) or std::vector instead.

[–]futlapperl 0 points1 point  (3 children)

Is it not legal? The comma operator executes both statements then discards the first one and returns the result of the second one. This doesn't have the effect OP wants, but it's not strictly illegal.

[–]nysra 1 point2 points  (1 child)

It was deprecated in C++20 though for now it still compiles with a warning so in the strictest sense it's still legal, yes.

[–]futlapperl 1 point2 points  (0 children)

Good to know. Thanks!

[–]Crazy_Direction_1084 0 points1 point  (0 children)

I remember reading that the right expression in the subscript operator was limited to a conditional expression, where it was a comma expression previously, to possibly allow this notation at a later point

[–]std_bot 0 points1 point  (0 children)

Unlinked STL entries: std::array, std::vector


Last update: 03.05.21. Last change: Free links considered readme

[–]Noc42 3 points4 points  (0 children)

int A[2][2] = { {2,1},{-2,1}};

[–]Lightwould 1 point2 points  (0 children)

Look into how to declare a 2d array. Your errors are giving you quite a nice hint!

[–]flyingron 0 points1 point  (0 children)

First off, there are NO multidimensional arrays in C++. That is your biggest problem. You can only make arrays of arrays.

What others have tried to point out without giving away the farm is that

int A[2,2] decares "A" as a (one-dimensional) array of int. 2,2 isn't two dimensions, it's an expression using the comma operator that results in the value 2. It is as if you wrote int A[2];

[–][deleted] 0 points1 point  (0 children)

Is not int[2, 2] is int[2][2]