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!

Recommended Answers

All 10 Replies

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>
}

Just use the array name when you call your function.

getMove( move, space );

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

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

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.

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.

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)

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

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.

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);

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.