-2

Could Someone explain what really happening in the below mentioned array. I have no idea what really the statement inside the for-loop does.Could someone Explain

#include<iostream>
using namespace std;

    int main(){
        
        int m=3,n=2;
        
        int *arr[m];

        for(int i=0;i<m;i++){
            arr[i]=new int[n];
        }
        
        return 0;
    }
  • 1
    try examples in https://en.cppreference.com/w/cpp/memory/new/operator_new – rohitt Apr 11 '21 at 10:35
  • `int m=3; int *arr[m];` is not standard C++ as `m` is not a compile-time constant. Some compilers will allow this as an extension, but it is not portable and should not be relied on. Make `m` be `const`, or else use `std::vector`. See [Why aren't variable-length arrays part of the C++ standard?](https://stackoverflow.com/questions/1887097) – Remy Lebeau Apr 11 '21 at 10:39
  • *Could Someone explain what really happening in the below mentioned array* -- What really happens is a compiler error. That array is not valid C++. – PaulMcKenzie Apr 11 '21 at 11:17

1 Answers1

2

arr is an array of pointers to int. So in the for-loop, you're creating an array of n int whose the first element is pointed by arr[i].

albator
  • 93
  • 7