I'm reading a book about C++. I'm on arrays now. It seems that a two-dimensional array is taken in this way:

void Function( int arr[][numberof2nddimension] ) {

Why is that so? Help please. Thanks

Re: 2-dimensional array as argument 80 80

a Very clear explanation can be found HERE

commented: I can't see anything at that link that answers the question about 2D arrays as parameters for functions -3
Re: 2-dimensional array as argument 80 80

I don't see it.

commented: Please see my new post. +14
Re: 2-dimensional array as argument 80 80

One way to think about it. A 2D array is basically an array of arrays. In the same way that an array of integers when passed to a function doesn't need the size itemized, it just needs to know what is contained in the array, a bunch of integers,of which the size of each integer is known , the 2D array doesn't need the size itemized it just needs to know what is in the array, a bunch of arrays , of which the size isn't known so it has to be supplied.

Hope this helps.

Re: 2-dimensional array as argument 80 80

I'm going to read your message a couple of times untill I understand :P Thanks

Re: 2-dimensional array as argument 80 80

@James and Xozz, the question was about an explanation around 2d arrays, which I subsequently showed in my link but it seems that the heading was seen about multi-dimensional arrays and page was closed. If the effort was taken to scroll down a little, the following can be found -

Two-Dimensional Array
Two – dimensional array is the simplest form of a multidimensional array. We can see a two – dimensional array as an array of one – dimensional array for easier understanding.

The basic form of declaring a two-dimensional array of size x, y:

data_type array_name[x][y];

data_type: Type of data to be stored. Valid C/C++ data type.
We can declare a two dimensional integer array say ‘x’ of size 10,20 as:

int x[10][20];

Elements in two-dimensional arrays are commonly referred by


where i is the row number and ‘j’ is the column number.
A two – dimensional array can be seen as a table with ‘x’ rows and ‘y’ columns where the row number ranges from 0 to (x-1) and column number ranges from 0 to (y-1). A two – dimensional array ‘x’ with 3 rows and 3 columns is shown below:
Initializing Two – Dimensional Arrays: There are two ways in which a Two-Dimensional array can be initialized.
First Method:

int x[3][4] = {0, 1 ,2 ,3 ,4 , 5 , 6 , 7 , 8 , 9 , 10 , 11}

The above array have 3 rows and 4 columns. The elements in the braces from left to right are stored in the table also from left to right. The elements will be filled in the array in the order, first 4 elements from the left in first row, next 4 elements in second row and so on.

Better Method:

int x[3][4] = {{0,1,2,3}, {4,5,6,7}, {8,9,10,11}};

This type of initialization make use of nested braces. Each set of inner braces represents one row. In the above example there are total three rows so there are three sets of inner braces.

Accessing Elements of Two-Dimensional Arrays: Elements in Two-Dimensional arrays are accessed using the row indexes and column indexes.

int x[2][1];

The above example represents the element present in third row and second column.

Note: In arrays if size of array is N. Its index will be from 0 to N-1. Therefore, for row index 2 row number is 2+1 = 3.

To output all the elements of a Two-Dimensional array we can use nested for loops. We will require two for loops. One to traverse the rows and another to traverse columns.

// C++ Program to print the elements of a 
// Two-Dimensional array 
using namespace std; 

int main() 
    // an array with 3 rows and 2 columns. 
    int x[3][2] = {{0,1}, {2,3}, {4,5}}; 

    // output each array element's value 
    for (int i = 0; i < 3; i++) 
        for (int j = 0; j < 2; j++) 
            cout << "Element at x[" << i 
                 << "][" << j << "]: "; 
            cout << x[i][j]<<endl; 

    return 0; 


Element at x[0][0]: 0
Element at x[0][1]: 1
Element at x[1][0]: 2
Element at x[1][1]: 3
Element at x[2][0]: 4
Element at x[2][1]: 5

So, as can be imagined, I am not very happy to be downvoted for doing or showing some effort in helping another!

@Xozz, I hope this helps out to a better understanding.

Re: 2-dimensional array as argument 80 80

It's commendable that you took the time to reply. However, the topic title and the origional post clearly asks for a discussion of 2-dimensional array as argument, and the content you posted covers declaration, referencing, and initialisation, but does not address that question.

commented: I came to realise the same, my apologies for jumping to conclusions, the error was from my side. +14
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.