Two dimensional arrays

Hello all, can someone please help me get the function disparray to compile.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//Simpe code on passing arrays into other functions for modification

#include <iostream>
using namespace std;

void dispArray(int **array)
{
    for ( unsigned int i = 0; i < 2; i++)
    {
		for ( unsigned int j =0; j < 3; j++)
			cout << array[i][j] << endl;   
    }
}

int main()
{
	int myArray[2][3] = {1,2,3,4,5,6};
	dispArray(myArray);
	
	//cout << myArray[0][1] ; 
}


-Thanks!
You should setup your 2d array with something like this int myArray[2][3] = { {1,2,3}, {4,5,6}}; Also to you can't pass a 2d array like that. The compiler needs to know how many columns there are in each row. So something like this void dispArray(int array[][3]) I would also suggest avoid using magic numbers such as 2 and 3.
Thanks for the reply giblit, but the problem is i do not know the number of the thing in my array at runtime, how would i overcome this?
If you don't know the size of the array at run time I think there is a problem with your program. Maybe you mean you don't know the size at compile time? For that you could either use templates or pass the size to the function.
Have you considered std::vector instead of the array?

It is possible to do what you want, if you're willing to use a little pointer magic.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;

void dispArray(int *array, size_t col, size_t row)
{
    for(size_t i = 0; i < col; i++)
    {
        for(size_t j =0; j < row; j++)
            cout << *(array + i + j) << " ";
      cout << endl;
    }
}

int main()
{
    int myArray[2][3] = {{1,2,3},{4,5,6}};

    dispArray(myArray[0], 2, 3);
}


And remember Variable Length Arrays are not supported by the current C++ standard, so if you don't know the size of the array at compile time you'll either need to use new/delete to create your array or use std::vector instead.



You can Dynamically Allocate Memory to 2D Array like this:

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
#include <iostream>
using namespace std;

void printArray(int**,int,int);

int main()
{
    int **arr;
    
    /*
     * Take Input from anywhere after the program starts (RUNTIME)
     * For Demonstration I am taking r=2 and c=3
     * */
    int r=2,c=3;
    
    //Dynamic Allocation of a 2D Array
    arr = new int*[r];

    for(int i=0;i<r;i++)
    {
        arr[i] = new int[c];
    }

    //Initializing 2D Array by 0
    for(int i=0;i<r;i++)
        for(int j=0;j<c;j++)
            arr[i][j] = 0;

    printArray(arr,r,c);

    cout << endl;
    return 0;
}

void printArray(int **inp, int r, int c)
{
    for(int i=0;i<r;i++)
    {
        for(int j=0;j<c;j++)
            cout << inp[i][j];
        cout << endl;
    }
}
Thanks Atyab & jlib, i was looking for something like that :).
Last edited on
I really recommend you think about using std::vector, it'll be much cleaner and less error prone.
Topic archived. No new replies allowed.