0

Given a double array of random values mod 5 and a value X and a position in the array, I have to change the position's value and all its neighbors to X. I'm doing it recursively. I think the idea is correct, however I must be messing with the return statement and the assignment using the recursive call. When compiling I get note: expected 'int **' but argument is of type 'int (*)[8]'

also, if I use the function in the main I get warning: passing argument 1 of 'floodfill' from incompatible pointer type table = floodfill( table, i, j, r );

error: assignment to expression with array type table = floodfill( table, i, j, r );

where table is the table on which the algorithm has to be executed

int ** floodfill ( int **tab, int i, int j, int v)
{
  /*if statement to make the values to the right of the specified position
  equal to the specified value*/
  if ( tab[i][j+1] == tab[i][j] )
  {
      /*the assignment is recursive*/
       tab = floodfill ( tab, i, j+1, v);
  }
  /*this assignment is executed after each of the values to the
  right of tab[i][j] are changed to v*/
  tab[i][j] = v;

  /*returns the tab with the specified position changed*/
  return tab;
}

Obviously the code is incomplete (no malloc, no check for out-of-bound position and only floodfill of right values) for the sake of brevity , but for what concerns my problem there should be everything.

Tom
  • 23
  • 4
  • 1
    What's your question? That's not clear. – Carcigenicate May 20 '17 at 12:44
  • How is `table` declared? Are you confusing a 2d array and a pointer to pointer to T? The warning indicates that that is the case. Read http://stackoverflow.com/questions/7586702/is-2d-array-a-double-pointer – Ilja Everilä May 20 '17 at 13:08

1 Answers1

1

Firstly, C pointers point to buffers which you can manipulate in place. They don't pass whole buffers by value. Returning an int ** is pointless, caller already has the information.

Secondly, you want to floodfill a value u with a new value, v. u might be the pixel value you first call, in which case the fist call is special. Easier to pass it in - so the function converts all values u and neighbours to value v. If the value to tab[i][j] is not u, return. Otherwise floodfill in all four directions. It's a bit heavy on the stack, but should work

void floodfill(int **tab, int u, int v, int i int j)
{
   if(tab[i][j] == u)
   {
       tab[i][j[ = v;
       floodfill(tab, u, v, i+1, ,j);
       floodfill(tab, u, v, i-1, j);
       floodfill(tab, u, v, i, j -1);
       floodfill(tab, u, v, i, j +1);
   }
} 
Malcolm McLean
  • 6,258
  • 1
  • 17
  • 18