10 Replies - 66795 Views - Last Post: 14 October 2006 - 11:39 PM Rate Topic: -----

#1 chrisfields  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-September 06

Passing A 2D Array to a function..

Posted 10 October 2006 - 06:32 PM

void populateSquareMatrix(int[],int);//functionp prototype//

int SquareMatrix[ArraySize][2];//2D array//


when i try to pass the array shown above to the function populateSquareMatrix i get a syntax error:

Quote

>>: error C2664: 'populateSquareMatrix' : cannot convert parameter 1 from 'int [20][2]' to 'int []'

if i include a second set of [] to the prototype that causes even more errors how would i pass a 2d array to the function?

this is how i call how i call the function.

populateSquareMatrix(SquareMatrix,ArraySize);

Is This A Good Question/Topic? 0
  • +

Replies To: Passing A 2D Array to a function..

#2 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Passing A 2D Array to a function..

Posted 10 October 2006 - 07:20 PM

http://www.cplusplus...ial/arrays.html

When passing a two dimensional array to a function in that manner, C++ expects to know the number of elements in the second dimension, like so
//call the function
myArrayFunc(myarr[][10]);


There are workarounds however...you can use vectors, or even pass the array as a pointer.
Was This Post Helpful? 0
  • +
  • -

#3 chrisfields  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-September 06

Re: Passing A 2D Array to a function..

Posted 10 October 2006 - 07:45 PM

populateSquareMatrix(SquareMatrix[][2],ArraySize);

gives error:

Quote

'

Was This Post Helpful? 0
  • +
  • -

#4 gregoryH  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 60
  • View blog
  • Posts: 656
  • Joined: 04-October 06

Re: Passing A 2D Array to a function..

Posted 11 October 2006 - 01:51 AM

View Postchrisfields, on 10 Oct, 2006 - 07:45 PM, said:

populateSquareMatrix(SquareMatrix[][2],ArraySize);

gives>>c:\error C2059: syntax error : ']'


how did you set up the signature of the function?

the alternate is to pass as pointer to pointer ( TYPE ** variable ), but you need a little more house keeping data to go with that.
Was This Post Helpful? 0
  • +
  • -

#5 chrisfields  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-September 06

Re: Passing A 2D Array to a function..

Posted 11 October 2006 - 11:38 AM

//function definition//
void populateSquareMatrix(int array[][2],int size)
{
statements.....
}



this way worked i also had to include it in the function definition itself
Was This Post Helpful? 0
  • +
  • -

#6 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Passing A 2D Array to a function..

Posted 11 October 2006 - 04:09 PM

This holds true for any function...the protottype much match the definition which must match the use.
Was This Post Helpful? 0
  • +
  • -

#7 chrisfields  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-September 06

Re: Passing A 2D Array to a function..

Posted 12 October 2006 - 09:24 AM

View PostAmadeus, on 11 Oct, 2006 - 04:09 PM, said:

This holds true for any function...the protottype much match the definition which must match the use.


void populateSquareMatrix(int array[ArraySize][2],int size)
{
	for(int i = 0; i < size; i++)
	{
		for(int j = 0; j < 2; j++)
		{
			printf("Row %d, Col %d: ",i,j);
			SquareMatrix[i][j]=(rand()%10);
			//SquareMatrix[][j](rand()%10);
			printf("%d,%d ",SquareMatrix[i][j]);
			printf("\n");
		}
	}



i am trying to store in 20 rows.... two numers in the 2 columns for each row
eg. row[0]=12,23
row[1]=34,56 etc etc

This post has been edited by Dark_Nexus: 31 October 2006 - 11:14 PM

Was This Post Helpful? 0
  • +
  • -

#8 amitgtbit  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-January 06

Re: Passing A 2D Array to a function..

Posted 13 October 2006 - 01:57 AM

Just pass the array as function_name(arr[][],int);

and reciverd it as the pointer and than just perform the normal poniter operations as an array you will get all the answers their and if u wan a confirm than use book named pointers in C by yashwant Kanitkar.
Was This Post Helpful? 0
  • +
  • -

#9 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Passing A 2D Array to a function..

Posted 13 October 2006 - 10:52 AM

Quote

Just pass the array as function_name(arr[][],int);


You need to specify the number of columns for the 2D Array.
Many people make this mistake.

To know why C/C++ requires us to do this, Click Here to visit my Blog for the answer.
Was This Post Helpful? 0
  • +
  • -

#10 chrisfields  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-September 06

Re: Passing A 2D Array to a function..

Posted 14 October 2006 - 01:40 PM

//function definition//
void populateSquareMatrix(int array[][2],int size)
{
	for(int row = 0; row< size; row++)
	{
		
		for(int col= 0; col< 2; col++)
		{
			SquareMatrix[row][col]=(rand()%10);
			printf("Row:%d\tCol:%d = %d ",row,col,SquareMatrix[row][col]);
			printf("\n");
		}
		
	}

}


yup it worked this way.....now here comes te hard part
Was This Post Helpful? 0
  • +
  • -

#11 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Passing A 2D Array to a function..

Posted 14 October 2006 - 11:39 PM

Quote

//function definition//

I hope you know that // considers the entire line as a comment and cannot be closed by using // again (unlike /* ... */ )


Quote

now here comes te hard part

Which part are to referring to now?
Was This Post Helpful? 1

Page 1 of 1