簡體   English   中英

類型“ int”的參數與參數類型“ int **”不兼容

[英]argument of type “int” is incompatible with parameter type “int **”

我正在編寫一個2D數組程序,但在打印時遇到問題,我不確定我是否正在正確地通過2D數組,因為它崩潰了而不是運行了。 任何意見將是有益的

void initialize(int* one, int** two);
void replace(int* arr,int rows, int cols,int value);
void fill(int* arr, int rows, int cols);
void print(int** arr, int rows, int cols);

ofstream outfile;
ifstream infile;
int arrayOne[100][100];
int arrayTwo[100][100];

int main(){

    int rows,cols=0;

    cout << "Please input how many rows you would like in the array:  ";
    cin >> rows;
    cout << "Please input how many columns you would like in the array:  ";
    cin >> cols;

    fill(arrayOne[100][100],rows,cols);
    //print(arrayOne[100][100],rows,cols);

    system("pause");
    return 0;
}

void initialize(int* one, int* two){
    for(int i=0;i<100;i++){
        for(int j=0;j<100;j++){
            arrayOne[i][j]=0;
            arrayTwo[i][j]=0;
        }
    }
}

void replace(int* arr,int rows,int cols,int value){
    arr[rows][cols]=value;
}

void fill(int* arr, int rows, int cols){
    int i=0;
    for(int r=0; r < rows; r++){
        for(int c=0; c < cols; c++){
            replace(arr,r,c,i++);
        }
    }
}

void print(int** arr, int r, int c){
    for(int i=0;i<r;i++){
        for(int j=0;j<c;j++){
            cout << arr[i][j] << " ";
        }
        cout << endl;
    }
}

如果您閱讀該錯誤信息,則可以清楚地說明您的問題。 話雖如此,它並沒有明確說明如何解決它。 您正在沿着固定陣列的艱難道路前進...

/* arrayOne[100][100] This is an 'int' at the 101st row and 101st column.
 * It isn't an address to anywhere in the array, in fact it is just beyond
 * the end of your array.
 *
 * Regardless, even if it were a pointer, it would point to a location in memory
 * that is not yours. We count starting with 0 in C/C++. So if you'd like to
 * reference the 'whole' array  just pass it bare:
 */
fill (arrayOne, rows, cols);

/* Of course this means that you need to fix the definition of 'fill'
 * and 'replace'.
 */
void replace(int arr[100][100],int rows,int cols,int value){
    arr[rows][cols]=value;
}

/* As you can see this isn't going to be friendly */
void fill(int arr[100][100], int rows, int cols){
    int i=0;
    for(int r=0; r < rows; r++){
        for(int c=0; c < cols; c++){
            replace(arr,r,c,i++);
        }
    }
}

您還有其他問題,但是在遇到其他問題時可以提出這些問題。

將所有int * arr和int ** arr更改為int arr [100] []或arr [] [100]。 我不記得是哪一個。 但是,肯定是其中之一。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM