Arrays as parameter?

How to indicate the argument of a function to be an array?

Page 1 of 1

7 Replies - 569 Views - Last Post: 15 June 2009 - 10:36 AM Rate Topic: -----

#1 mumeisyuu  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 03-October 08

Arrays as parameter?

Post icon  Posted 15 June 2009 - 08:48 AM

Hi all,

I know this question might be a bit too "n00bie" but... I just couldn't figure it out ><"
The question is, when defining a function, how do I indicate that the received argument to be an array?

For example if I have something like...

char board[8][8];

//other codes

void myFunction( ... )
{
//function codes
}

as shown above, what should I fill in at the "..." part?
I tried something like void myFunction(char board) but then when compile it says can't convert correctly

So what exactly should I write?

I'd like to use other containers like vector if I could but I think in my case I'd need the 2D array...
Thanks for the help :D

Is This A Good Question/Topic? 0
  • +

Replies To: Arrays as parameter?

#2 jcmaster2  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 183
  • Joined: 27-April 09

Re: Arrays as parameter?

Posted 15 June 2009 - 08:50 AM

myFunction(board); //Function call

That simple - based on what you said...

This post has been edited by jcmaster2: 15 June 2009 - 08:51 AM

Was This Post Helpful? 0
  • +
  • -

#3 mumeisyuu  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 03-October 08

Re: Arrays as parameter?

Posted 15 June 2009 - 09:08 AM

View Postjcmaster2, on 15 Jun, 2009 - 07:50 AM, said:

myFunction(board); //Function call

That simple - based on what you said...


No no no~ I know when calling the function I would write myFunction(board);

What my question asking is...
When DEFINING the function, what should the argument type be?

void function(char board)?
void function(char board[][])?
void function(char array)?

or else? ><|||
Was This Post Helpful? 0
  • +
  • -

#4 Kanvus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 42
  • View blog
  • Posts: 452
  • Joined: 19-February 09

Re: Arrays as parameter?

Posted 15 June 2009 - 09:15 AM

#include <iostream>

using namespace std;

void myFunction(char hey[][8])
{

}
 
int main(){
	char board[8][8];
   
	myFunction(board);
	 
	system("PAUSE");
	return 0;   
}
 


The rule is, every [] after the first needs a number in it as boundaries.


yay

This post has been edited by Kanvus: 15 June 2009 - 09:16 AM

Was This Post Helpful? 0
  • +
  • -

#5 kidicarus  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 139
  • Joined: 13-February 09

Re: Arrays as parameter?

Posted 15 June 2009 - 09:35 AM

In C++ you pass a complete block of memory as an address, not as a value, as you do in java. To pass a simple array, since arrays and pointers are so similar, is to do this:
void procedure (int arg[])

One way to pass a multidimensional array would be to specify the depth, like so:

void procedure (char myArray[][5], int size); //function header
http://www.cplusplus...utorial/arrays/

where "size" is the number of rows.

You could also do it like so:

#include <iostream>

void ChangetoB(char *a, int r, int c);




int main()
{
	
	char a[5][5];
	
	for (int i = 0; i < 5; i++)
		for (int j = 0; j < 5; j++)
			a[i][j] == 'a';
	
	char array[5][5];
	ChangetoB(a[0], 5, 5); // call using array dereferencing
}

void ChangetoB(char *a, int r, int c)
{
	
	for(int i=0; i<r; i++)
	{
		for(int j=0; j<c; j++)
		{
			a[i*c+j] = 'b';
		}
	}
}




And the function will change the elements from a to b
Was This Post Helpful? 0
  • +
  • -

#6 kidicarus  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 139
  • Joined: 13-February 09

Re: Arrays as parameter?

Posted 15 June 2009 - 09:48 AM

An even cooler thing to do would be to create a dynamic array, and then pass in the first element as a pointer to char, as well as the rows and columns.
Was This Post Helpful? 0
  • +
  • -

#7 Kanvus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 42
  • View blog
  • Posts: 452
  • Joined: 19-February 09

Re: Arrays as parameter?

Posted 15 June 2009 - 10:11 AM

sounds good too. dont forget to limit size no matter which method you pick
Was This Post Helpful? 0
  • +
  • -

#8 mumeisyuu  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 03-October 08

Re: Arrays as parameter?

Posted 15 June 2009 - 10:36 AM

Thanks for all the replies and help >.< *appreciate*

I would look into the method suggested and give a try myself :D
If I still can't get it right I'd like to ask again >~<"

Thanks again!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1