954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Passing an array to a function

I'm trying to pass an array to a function for use, but it won't compile, it gives me the error:

>Type error in argument 2 to 'getMove'; found 'int' expected 'int *'

It is an int array, and it won't let me pass it. From what I can guess its a pointer thing, but I don't know how to fix it. If the code helps, here it is:

1. void getMove(int move, int space[9]);
2. getMove(move, space[9]);
3. void getMove(move, space[9])


1. is the prototype
2. is a function call in the program
3. is the function start.

All I can guess is I need a pointer to pass it, any help would be greatly appreciated, thanks!

Firestone
Light Poster
37 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

The declaration should be

void getMove( int move, int space)
{
<snippet>
}


if you want to pass the whole array as a parameter then it should be:

void getMove( int move, int space[] )
{
<snippet>
}
Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

Just use the array name when you call your function.

getMove( move, space );
Hamrick
Posting Whiz
325 posts since Jun 2007
Reputation Points: 180
Solved Threads: 34
 

Ok it works now. I use both your suggestions by changing the declaration and the call, thanks!

Firestone
Light Poster
37 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

int space [] is in effect the same as int *space, you can use one instead of another.

TkTkorrovi
Junior Poster
170 posts since Mar 2005
Reputation Points: 85
Solved Threads: 13
 
int space [] is in effect the same as int *space, you can use one instead of another.

Sorry, but the two are not interchangeable.int space [] causes illegal syntax error message (VC++ 2005 Express compiler.). Unless that is something new in C99 standards, which very few compilers support yet.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

But they must be interchangeable in gcc, i never use such construct, but the parameters of the function main are often written as int argc, char *argv [], and one can also write char **argv.

TkTkorrovi
Junior Poster
170 posts since Mar 2005
Reputation Points: 85
Solved Threads: 13
 
Sorry, but the two are not interchangeable. int space [] causes illegal syntax error message (VC++ 2005 Express compiler.). Unless that is something new in C99 standards, which very few compilers support yet.

Hmm, I just tried that on Comeau in strict C90 mode (Comeau tends to be fully standards compliant AFAIK), and it claimed to have compiled successfully with no errors.

void foo(int bar[])
{
}

int main()
{
    int arr[5];
    foo(arr);
}


http://www.comeaucomputing.com/tryitout/

Is Comeau wrong? or were you referring to variable declarations outside of function parameters? (Where I don't believe any version of the C standard allows empty subscript brackets)

Bench
Posting Pro
577 posts since Feb 2006
Reputation Points: 307
Solved Threads: 63
 

The standard even allows empty subscript brackets in variable declarations outside function parameters, but there they must be followed by the initializer

TkTkorrovi
Junior Poster
170 posts since Mar 2005
Reputation Points: 85
Solved Threads: 13
 
int space [].

That syntax is ok when declared as a parameter to a function, but not when declaring an object within the body of a function. I may (probably) misinterpreted your post.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

I'm trying to pass an array to a function for use, but it won't compile, it gives me the error:

>Type error in argument 2 to 'getMove'; found 'int' expected 'int *'

It is an int array, and it won't let me pass it. From what I can guess its a pointer thing, but I don't know how to fix it. If the code helps, here it is:

1. void getMove(int move, int space[9]);
2. getMove(move, space[9]);
3. void getMove(move, space[9])

1. is the prototype 2. is a function call in the program 3. is the function start.

All I can guess is I need a pointer to pass it, any help would be greatly appreciated, thanks!

From Narendhar,
In the function call you need to call

getMove(move,space);

And the function start should be

void getMove(int move,int *space);

narendharg
Newbie Poster
12 posts since May 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You