So I stuck at the pset3, at the place where you need to implement game of fifteen. How can I pass multidimensional array as an function argument? And how can I return multdimesional array as a function's return value?

I want to use one function to produce a 2 dimesional value and then use it as an argument for second function. But I keep getting error messages

incompatible pointer types returning 'int [d][d]' from a function with result type 'int **' [-Werror,-Wincompatible-pointer-types]

What might be the problem - the code outline looks as below

int** function1(); //Declare function that returns two dimesional array; 

void function2();//declare second function

int main(void)
   function2(fuinction1()); //run a function2 which uses function 1 output as an argument 

 int ** function1(void) //declare function11 as returning twodimesional array

  return 2_dimesional_array; 

    void function2(2_dimesional_array**) //function 2 uses function 1 return value
        //code magic here
  • If anyone is confused while trying to understand how 2d arrays work in functions - as I am - here is an interesting discussion: stackoverflow.com/questions/14088804/… ;I do not understand everything from this discussion but is instructive. – McCzajnik Jan 2 '17 at 3:04

Your solution seems unnecessarily cumbersome. However, to answer your question, a function cannot return an entire array. But you can certainly return a pointer to an array by declaring it as follows:

int *pointer[d][d];

You might want to consider a simpler way of going about this problem, check out this thread: pset3-fifteen-how-to-identify-board-edges-in-move-function

| improve this answer | |
  • Thanks! Yes now Ive got it. And yes - I can see your point - I was doing it all the way around... – McCzajnik Jan 2 '17 at 3:36
  • I have been having the exact same problem as McCzajnik (and taking the same approach - trying to get one function to initialise a 2-d array, then pass it to main / another function to print out)... I've read through all the comments on this thread, and linked threads... And I've tried to follow @ronga 's suggestion of taking a different approach, but I just don't see it. What is the point of having a function to initialise a 2-d array if you can't then use those values? : / Is there any other reading material someone can point me to? – StephanieJosine Jan 10 '17 at 15:35
  • Iterate through the array using 2 loops, 1 for i and nested within it another for j, you're looking for the i, j values where board[I][j]=tile you want. – ronga Jan 11 '17 at 1:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .