What is this array problem

As stated in c++, array name is actually a pointer, then a multidimensional array's name is also a pointer which point to the first value in the array.

when I want to transfer a multidimensional array to a function, why do I need to specify the second dimension like this: printArray(a2[][5],length)??? Does multidimensional array's name have any difference between a simple array???

the other question: the following code cannot be exed, there are problems on both lines, what is it? I am using gcc current version compiler. Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;

bool printArray(int arg[],int length) {

	for(int n=0; n<length; n++) {
		cout<<arg[n]<<" ";
	}
	cout<<endl;
	return true;
}


int main() {
	int a1[]={0,1,2,3,4};
	int a2[][5]={0,1,2,3,4,5,6,7,8,9};
	printArray(a1,5);
	printArray(a2,10); //Problem Line
	printArray(a2[][5],10); //Problem Line
	return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
bool printArray(int *arg,int length) {

	for(int n=0; n<length; n++) {
		cout<<arg[n]<< " ";
	}
	cout<<endl;
	return true;
}

int main() 
{
	int a1[]={0,1,2,3,4};
	int a2[2][5]={0,1,2,3,4,5,6,7,8,9};
	printArray(a1,5);


	for (int k = 0; k < 2; k++)
		printArray(a2[k], 5);


	return 0;


I hope it helps you.
Thanks, firix, that does output what I want? but could you explain a bit to me:

why do I need to specify the dimension of an array? now that the array name is a pointer and point to the first one of the array. Isn't it the same for a one dimension array name and an two dimension array name? they are all pointers and point to the same type of data, isn't it?
Hi northfly,

a2 is a matrix

matrix`s structure:

for example:

matrix[3][3];

0 :0 1 2 // The first Array (an array with three elements)
1 :0 1 2 // The second Array (an array with three elements)
2 :0 1 2 // The third Array (an array with three elements)



Topic archived. No new replies allowed.