0
#include <iostream>
using namespace std;

int main() {

    int n;
    cin >> n;

    int m[n][2];
    for (int i = 0; i < n; i++) {
        cin >> m[i][0];
        cin >> m[i][1];
    }
}

if you run this codes in visual studio, then i get errors at line 9. Can anyone tell why i have errors?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
chan
  • 1
  • 2
    Change `int m[n][2];` to `std::vector> m;` – Eljay Sep 14 '20 at 12:54
  • which is line 9? Also mention the error. Is that #include empty? – vinod Sep 14 '20 at 12:54
  • @Eljay take care, if thats the only modification, the code will have errors and UB lurking around the corner – 463035818_is_not_an_ai Sep 14 '20 at 12:56
  • @vinod int m[n][2]; this is line number 9 – chan Sep 14 '20 at 13:00
  • can u paste the error because i cant find any error, code is completely running. Why the post is closed? VLA issues were in earlier versions. Daily i declare VLA and use them without any errors. – vinod Sep 14 '20 at 13:02
  • 1
    @vinod why do you do that? There is practically zero need for VLAs in C++. There is an error because VLAs are not part of standard C++, the come from C and some compilers provide them as extension also in C++, though also in C they became optional, so support in C++ may also disappear at any time – 463035818_is_not_an_ai Sep 14 '20 at 13:11
  • did u try running the code? I cant find any error in vscode or ideone or any other c++ compilers. U can use vector and it is better but we can use VLAs also without any errors. – vinod Sep 14 '20 at 13:18
  • @idclev463035818 • no, that wouldn't be the only change in isolation. The rest of the code would have to be changed to accommodate. – Eljay Sep 14 '20 at 14:18

1 Answers1

-1

It looks like you're trying to initialize m as a 2d array with the first dimension's size as user input, and the second as constant 2. To do that you can use

int n;
cin >> n;

int (*m)[2] = new int[n][2];

for (int i = 0; i < n; i++) {
    cin >> m[i][0];
    cin >> m[i][1];
}
  • but why did you add [2] behind (*m) – chan Sep 14 '20 at 13:36
  • because it's a 2d array, the first dimention is dynamically allocated with n, but the second is a constant 2. If you wanted both to be dynamically allocated you could int **m = new int[length][length]; – user14150338 Sep 15 '20 at 14:19