2014-02-16 19 views
0

當我嘗試銷售數組傳遞給函數,我得到這個:錯誤爲什麼在將這個二維數組傳遞給一個函數時出現錯誤?

C2664: 'printArray':無法從 'INT [4] [5]' 轉換參數1到 '廉政'

這裏的陣列,並呼籲:

int sales[4][5], row, column; 

    for (row = 0; row < 4; row++) 
    { 
     for (column = 0; column < 5; column++) 
     { 
      cin >> sales[row][column]; 
     } 
    } 

printArray(sales); 

與這裏的功能:

void printArray(int A[4][5]) 
{ 
    for(int R=0;R<4;R++) 
    { 
    for(int C=0;C<5;C++) 
     cout<<setw(10)<<A[R][C]; 
    cout<<endl; 
    } 
} 

在此先感謝。

+0

函數原型應該只是printArray(int A [] [5]) – epx

+0

http:// stackoverf low.com/questions/8767166/passing-2d-array-to-function –

+0

http://stackoverflow.com/questions/8767166/passing-2d-array-to-function 看一看這個 – pa1geek

回答

2

試試這個

void printArray(int A[][5]) 
{ 
    for(int R=0;R<4;R++) 
    { 
    for(int C=0;C<5;C++) 
     cout<<setw(10)<<A[R][C]; 
    cout<<endl; 
    } 
} 

希望這有助於.. :)

編輯: 還有一些其他的方式來做到這一點。以爲我分享給你:

你可以傳遞一個指針數組。

void printArray(int *A[4]) 

您可以將指針傳遞給指針。

void printArray(int **A) 
0

你應該修改printArray功能是這樣的:

void printArray(int *A, int row, int col) 
{ 
    for(int R=0;R<row;R++) 
    { 
    for(int C=0;C<col;C++) 
     cout<<A[R * col + C] << endl; 
    } 
} 

然後調用這個函數如下所示:

printArray(&sales[0][0], 5, 5); 

注意你通過行和列數的值函數

+0

謝謝,但我使用該代碼時出現此錯誤:錯誤C2664:'printArray':無法將參數1從'int *'轉換爲'int [] [5]' – user3053293

+0

您如何調用函數?您必須傳遞銷售地址[0] [0] –

相關問題