Here's a problem about arrays and pointers;

closed account (1yvU5Di1)
On my leisure, I was looking in random college work repos on github and I came across one problem (probably simple) that I couldn't solve.

Write a function that is given two positive integers r, c and allocates a 2-dimensional array with
 r rows and c columns. The function then returns a pointer to the allocated array.


This is quite frustrating... for someone with a good knowledge of arrays and pointers, I don't even understand what was asked here.

Has anyone probably solved this somewhere or can you cook up a sample code that can elaborate or probably answer this question?

Thank you.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>

int *TwoDArray(int r, int c);

using namespace std;

int main() {

    TwoDArray(10, 10);

}

int *TwoDArray(int r, int c) {
    int **ary = new int *[r];
    for (int i = 0; i < r; ++i)
        ary[i] = new int[c];
}


http://stackoverflow.com/questions/936687/how-do-i-declare-a-2d-array-in-c-using-new
Last edited on
closed account (1yvU5Di1)
Well done.
The stackoverflow link says it all.
The question was a little vague IMO.
Topic archived. No new replies allowed.