[英]Is this the correct way of initializing a variable-sized array
我想創建一個可變大小的二維數組,行數為 m,列數為 n。 m 和 n 的第 th 個值是在執行期間從用戶處獲取的。 以下方法有效並產生輸出。 現在讓我知道這在與 cpp 相關的所有場景中在技術上是否可行。
#include<iostream>
using namespace std;
int main() {
int **arr, m,n;
cout<<"enter";
cin>>m>>n;
arr = new int*[m];
for(int i = 0; i < m; i++) {
arr[i]=new int[n]; // initializing a variable-sized array
}
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
cout<<arr[i][j]; // printing the array
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.