2017-05-20 41 views
0

給定一個隨機值mod 5的雙數組和一個值X以及數組中的一個位置,我必須將位置的值及其所有鄰居改爲X. 我正在遞歸地執行它。我認爲這個想法是正確的,但是我必須搞清楚return語句和使用遞歸調用的任務。 當編譯我得到 注:應爲「INT **」,但參數的類型爲「INT(*)[8]」洪水填充算法C - 返回雙數組?

另外,如果我在主要使用功能我得到 警告:傳遞的參數1來自不兼容指針類型的'floodfill' table = floodfill(table,i,j,r);

錯誤:賦值給數組類型表達式 table = floodfill(table,i,j,r);

其中表是其上該算法被執行

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

表顯然的代碼是不完整的(沒有malloc的,不檢查是否超出限制的位置,並且僅floodfill右值),用於爲了簡潔起見,但對於我所遇到的問題,應該有一切。

+1

你的問題是什麼?這並不清楚。 – Carcigenicate

+0

如何聲明'table'?你是否會混淆2​​d數組和指向T的指針?警告表明情況就是如此。閱讀http://stackoverflow.com/questions/7586702/is-2d-array-a-double-pointer –

回答

1

首先,C指針指向緩衝區,您可以在其中進行操作。他們沒有按價值傳遞整個緩衝區。返回一個int **是毫無意義的,調用者已經有了這些信息。其次,你想用新值填充一個值u,v。u可能是你第一次調用的像素值,在這種情況下,第一次調用是特殊的。更容易傳遞它 - 所以函數將所有值u和鄰居轉換爲值v。如果tab [i] [j]的值不是u,則返回。否則在所有四個方向進行填充。它有點沉重,但應該工作

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