views:

105

answers:

3

I know it's simple, but I can't seem to make this work.

My function is like so:

 int GefMain(int array[][5])
 {
      //do stuff
      return 1;
 }

In my main:

 int GefMain(int array[][5]);

 int main(void)
 {
      int array[1800][5];

      GefMain(array);

      return 0;
 }

I referred to this helpful resource, but I am still getting the error "warning: passing argument 1 of GefMain from incompatible pointer type." What am I doing wrong?

EDIT:

The code is in two files, linked together by the compiler. I am not using gcc. The above code is exactly what I have, except the function is declared as "extern int" in the main. Thank you all for your time.

+2  A: 

The code is fine. In a single file, this compiles fine for me with gcc.

int g(int arr[][5])
{
    return 1;
}

int main()
{
    int array[1800][5];
    g(array);
    return 0;
}

My guess is that you're #includeing the wrong file -- perhaps one that had a different declaration for GefMain. Or perhaps you just haven't saved the file that declared GefMain, so it still has an argument of int [][3], for instance, which would cause the warning.

I would suggest that you post the entire code to reproduce the problem (after you strip out everything that's unneeded to reproduce it, of course). But chances are, at that point, you'll have solved it yourself.

Mark Rushakoff
Yes... he's only getting a warning. And the question is why?
Simon
My code is in two files, actually. Would that change anything? The only difference in code is that the declaration in the main file has "extern int" instead of just int.
Evelyn
Same things... it gets compiled ok even with `-pedantic` option and extern int.
ShinTakezou
Okay, weird. Maybe I'm having compiler issues. I'll just ignore the warning for now. Thanks for the help.
Evelyn
I found the problem. Thanks for your suggestion, Mark. Stripping everything down to the bare minimum, the only difference between the above code and mine was the datatype of the array. My compiler uses a 16-bit integer instead of the usual 32. Switching to 32 fixed the issue.
Evelyn
@Evelyn: Stripping the problem down to the minimum reproducible case will often cleanly isolate what went wrong. It's a very good habit to develop.
Mark Rushakoff
A: 

gcc will have extensions for what you've had (and others have had sucess with). Instead try this, it'll be more portable to other c compilers:

int g(int (* arr)[5])
{
    return 1;
}

int main()
{
    int array[1800][5];
    g(array);
    return 0;
}

or better yet;

int g(int (* arr)[5], int count)
{
    return 1;
}

int main()
{
    int array[1800][5];
    g(array, sizeof(array)/sizeof(* array));
    return 0;
}

You're getting a warning because an array of any dimension becomes a pointer when it is passed as an arguement, the above gives the compiler a clue that it should expect such.

Jamie
It won't be "more portable". The OP's code is as standard and portable as it can ever get.
AndreyT
Unfortunately, I still get the same error.
Evelyn
and by the way the answer is not correct, since your code is perfect from the standard (and so portability) point of view (it does not use any gcc ext, and you are not using gcc at all!)! tell us which is the compiler you're using (and version) instead.
ShinTakezou
+1  A: 

It compiles rightly even with -std=c99 -pedantic options. And it looks right anyway... Is it really the code you want we check? Compiler you're using...?

ShinTakezou