Hi all I was wondering how to pass a multidimensional array to a function:

Here is my code:

#include <cstdlib>
#include <iostream>

using namespace std;

void PrintArray (int* array, int n); 

int main(){
	int SIZE;
		
	cout << "Enter how many records: ";
	cin >> SIZE;
	
	int array[SIZE][2];
	
	for (int i =0; i < SIZE; i++){
	cout << "Enter an Integer: ";
	cin >> array[i][0];
	cout << "Enter a user ID: ";
	cin >> array[i][1];
	}

	cout << endl << "The list you put in is: " << endl;
	PrintArray(array, SIZE);
	
	
	system("PAUSE");
}

/*	This function prints an Array
	Arguments:
						array - the array to be printed
						n - number of elements in the array
*/

void PrintArray(int* array, int n)
{
	int i;
	 
	for( i = 0; i < n; i++) {
	cout<< array[i][0]<<'\t';
	}
	
	cout << endl << endl;
	
	for( i = 0; i < n; i++) {
	cout<< array[i][1]<<'\t';
	}
	
}

The error I keep getting is:

cannot convert `int (*)[2]' to `int*' for argument `1' to `void PrintArray(int*, int)'

If it's a single dimension array then it compiles no problem. Please could someone tell me what I'm doing wrong.

Thanks for your time and trouble

When passing in a 2D array you must specify the second dimension in the parameter: void PrintArray(int array[][2],int n); I should point out that the size of a fixed sized array must be known at compile time, so what you are relying on is likely a compiler extension and is therefore non-standard. You need something like:

int ** a;
a = new int*[rows];
for(int i = 0; i < rows; i++)
    a[i] = new int[cols];    //since you columns is known I don't know if there's a 
                             //shortcut for this step, but I know this works

The added bonus of doing the dynamic allocation is that you can change your function signature to: void Printarray(int ** array,int n) (and you don't have to specify the second dimension).

Could you perhaps show me how to implement this in my code. I get the prototype defintion but not the part afterwards... where do I add that?

Sorry I'm relatively new to C++

Thanks.

Take out line 14, and change "a" to array (walk yourself through the code and try to understand what each of the lines is doing). Substitute in your SIZE variable for the number of rows, and 2 for the number of columns.


and you need to change both lines 6 and 36 to match the line at the bottom of my last post (since you have an int ** pointer, you don't have to worry about passing the second dimension in).

Incidentally, if you think that this approach is outside of the scope of what your instructor expects, certainly revert back to your other approach and use the int array[][2] method. I just wanted you to see the proper way to set up the dynamic 2D array.

#
void PrintArray(int* array, int n)

This is an argument for a single dimension array. How did you declare your array?

int array[SIZE][2];

Maybe that's how your function should see it too. Try

void PrintArray(int* array[][2], int n)

The first dimension can be blank and the compiler will fill it in for you. You must provide the size of the second dimension though. I just spent 2 days fighting with gcc trying how to figure the format it wanted based on error codes about an invalid int[int]** or something or other. Hope this helped.

#
void PrintArray(int* array, int n)

This is an argument for a single dimension array. How did you declare your array?

int array[SIZE][2];

Maybe that's how your function should see it too. Try

void PrintArray(int* array[][2], int n)

The first dimension can be blank and the compiler will fill it in for you. You must provide the size of the second dimension though. I just spent 2 days fighting with gcc trying how to figure the format it wanted based on error codes about an invalid int[int]** or something or other. Hope this helped.

alternatively, you might try using a vector of vectors

vector< vector<int> >

Maybe that's how your function should see it too. Try

void PrintArray(int[B]*[/B] array[][2], int n)

The first dimension can be blank and the compiler will fill it in for you.

You've got an extraneous star in there, int array[][2] instead of int * array[][2]

You've got an extraneous star in there, int array[][2] instead of int * array[][2]

As a complete noob, I will acquiesce to experience. I went back and looked at my project after your post. What I have is a 2d array of pointers, where the OP just had a 2d array. I stand corrected.

Be a part of the DaniWeb community

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