how to find out the length of an array?

I have a function returning a 2D array which the size dynamically created at runtime.

 
  int** getArray()


now after i call this function, how do i find out the length of the returned array?

thank you.
You will need to somehow return the array size from the function.

You can't. That's one of the disadvantages of accessing raw arrays via pointers. Consider returning a vector<vector<int>> instead.
closed account (48T7M4Gy)
Show us how you declare the array please.

closed account (48T7M4Gy)
While it is not as user convenient as using STL containers, particularly vectors, a 2d dynamic array can be organised quite simply to effectively 'know' its size. Once the dimensions are determined, like all c-style array manipulations they must be included in any subsequent function calls. Tedious but nevertheless quite easily done.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Modified from http://stackoverflow.com/questions/15908632/returning-a-two-dimensional-array-from-a-function-in-c

#include<iostream>

int** createTable(int &, int &);

void deleteTable(int**, int rows);
void printTable(int**, int rows, int);

int main()
{
    int rows = 0;
    int cols = 0;
    
    int** table = createTable(rows, cols);
    
    printTable(table, rows, cols);
    deleteTable(table, rows);
    
    return 0;
}

int** createTable(int &r, int &c)
{
    std::cout << "Enter rows cols:";
    std::cin >> r >> c;
    
    int** table = new int*[r];
    for(int i = 0; i < r; i++) {
        table[i] = new int[c];
        for(int j = 0; j < c; j++)
        {
            table[i][j] = (i+j);
        }// sample set value;
    }
    return table;
}

void deleteTable(int** table, int rows){
    if(table){
        for(int i = 0; i < rows; i++){ if(table[i]){ delete[] table[i]; } }
        delete[] table;
    }
}

void printTable(int** table, int rows, int columns){
    for(int i = 0; i < rows; i++){
        for(int j = 0; j < columns; j++){
            printf("(%d,%d) -> %d\n", i, j, table[i][j]);
        }
    }
}
Topic archived. No new replies allowed.