Declaring a pointer to multidimensional array and allocating the array

前端 未结 6 434
名媛妹妹
名媛妹妹 2020-12-02 10:32

I\'ve tried looking but I haven\'t found anything with a definitive answer. I know my problem can\'t be that hard. Maybe it\'s just that I\'m tired..

Basically, I wa

6条回答
  •  长情又很酷
    2020-12-02 10:59

    I just found this ancient answer still gets read, which is a shame since it's wrong. Look at the answer below with all the votes instead.


    Read up on pointer syntax, you need an array of arrays. Which is the same thing as a pointer to a pointer.

    int width = 5;
    int height = 5;
    int** arr = new int*[width];
    for(int i = 0; i < width; ++i)
       arr[i] = new int[height];
    

提交回复
热议问题