I have a project and I have to define an array of arrays of different dimensions (like a triangle) because I am not allowed to use std::vector or other container class. For this, I am using an array of pointers. Normally, I would do this:
int* triangle[n];
for(int i = 0; i < n; i++) {
triangle[i] = new int[i + 1];
for(int j = 0; j <= i; j++) cin >> triangle[i][j];
}
But I must not use dynamic memory! I thought that doing
int* triangle[n];
for(int i = 0; i < n; i++) {
int row[i + 1];
triangle[i] = row;
for(int j = 0; j <= i; j++) cin >> triangle[i][j];
}
would do the trick. But it didn't. Instead, when I iterate over the arrays and print the contents, I get garbage. So, how can I replace a dynamic allocation with a static one?