# using a 2d-array in a function

This topic is 4503 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

if you use an int you do this: void function(int a){ and use it like this: function(wich_int_you_want_to_use); but how to do this with a 2d-array like: int array[4][4]; becouse void funtion(int array[4][4]); does not seem to work, hope you will understand my question, hope to see an answer soon :-) greetings, BackwardsDown

##### Share on other sites
I think that doing void function(int (*a)[dim]) should work. You can also use void function(int a[][dim])

I think you need to specify the size of the second field.

EDIT: checked and then fixed

##### Share on other sites
posted a question, solved it

##### Share on other sites
I still dont understand, can someone edit my code please? it would be very appreciated :)

#include <cstdlib>
#include <iostream>

using namespace std;

int function(int a[][5]){
a[2][1] = 2;
return a[2][1];
}

int main()
{
int array[5][5];
int b;
b = function(array[5][5]);
cout<< b;
system("PAUSE");
return EXIT_SUCCESS;
}

##### Share on other sites
The following should work.

The following line b = function(array[5][5]); actually passes in the value of array[5][5].

#include <cstdlib>
#include <iostream>

using namespace std;

int function(int a[][]){
a[2][1] = 2;
return a[2][1];
}

int main()
{
int array[5][5];
int b;
b = function(array);
cout<< b;
system("PAUSE");
return EXIT_SUCCESS;
}

##### Share on other sites
Quote:
 Original post by BackwardsDownI still dont understand, can someone edit my code please? it would be very appreciated :)#include #include using namespace std;int function(int a[][5]){ a[2][1] = 2; return a[2][1]; }int main(){ int array[5][5]; int b; b = function(array[5][5]); cout<< b; system("PAUSE"); return EXIT_SUCCESS;}

The instruction array[5][5] actually retrives the value in the position [5][5]. You should be careful because in a [5]*[5] array there is not the element at the post [5][5] (index start from 0) and you are accessing random memory (WARNING!!).
To identify an array you just use its name ('array' in this case).

##### Share on other sites
A small note, array indexing starts at 0. If you make an array that is 5 elements long, the first element is indexed by 0, and the fifth one is indexed by 4. If you try to use 5 for the index, you will get garbage. It is a little strange because you use [] when you declare the size (5), and you also use it for indexing. So in one context you must use the size (5) and in others you must not use any more than size-1 (4).

If you want to pass a 1d array, you just use the array name (without []). If you want to pass a 2d array (an array of arrays) you must specify the number of arrays (the second index) in the function's parameters. Your function() parameters should look like this: function(int a[][5]). When you wish to call the function from main, you just use the array name.

Remember that 5 is the size of the array (the number of elements), but the fifth element is indexed by 4. Using 5 gives you garbage. This has to do with the way the arrays are stored, but that knowledge is less important than knowing how to use the arrays.

#include <iostream>#include <cstdlib>using namespace std;int function(int a[][5]) // Number of integer arrays will be 5{	a[2][1] = 2;	return a[2][1];}int main(){	int array[5][5];	int b;	b = function(array);	cout<< b;	system("PAUSE");	return EXIT_SUCCESS;}

##### Share on other sites
int* fun(int (*)[5]);int main(int argc, char **argv){	int arr[5][5];	int (*p)[5];	p = (int (*)[5])fun(arr);	getchar();	return EXIT_SUCCESS;}int* fun(int (*p)[5]){	return p[0];}

*** Another way ***:

typedef int (*T_pt2d)[5];T_pt2d fun(int (*)[5]);int main(int argc, char **argv){	int arr[5][5];	int (*p)[5];	p = fun(arr);	getchar();	return 0;}T_pt2d fun(int (*p)[5]){	return p;}