2017-04-21 29 views
-2
#include<stdio.h> 
int arr[12][5]; 


int score[12][5]; 
int n; 

void chk(){ 
    int score = 0; 
    for(int i=n-1;i>=0;i--){ 
     for(int j = 0;j<5;j++){ 
      scanf("%d",&(arr[i][j])); 
     } 
    } 


    for(int i = n-1;i>=0;i--){ 
     for(int j=0;j<5;j++){ 
      if(i==n-1) 
       score[i][j] = arr[i][j]; 
      else{ 
       int mx = score[i+1][j]; 
       if(j>0 && score[i+1][j-1]>mx) 
        mx = score[i+1][j-1]; 
       else if(j<4 && score[i+1][j+1]>mx) 
        mx = score[i+1][j-1]; 
       score[i][j] = arr[i][j] + mx; 
      } 
     } 
    } 

    int mx_score = score[0][2]; 
    if(score[0][1]>mx_score){ 
     mx_score = score[0][1]; 
    } 
    else if(score[0][3]>mx_score){ 
     mx_score = score[0][3]; 
    } 

    printf("%d",mx_score); 
} 

int main(){ 
    int T; 
    scanf("%d",&T); 
    for(int i = 0;i<T;i++){ 
     scanf("%d",&n); 
     chk(n); 
    } 
return 0; 
} 

編譯錯誤:這段代碼爲什麼會導致編譯錯誤? [C,全局變量,2D陣列]

Subscripted value is not an array, pointer or vector. 

導致該錯誤是score的變量。 我不明白爲什麼arr工作正常,但score沒有。 你會怎麼寫這個代碼更好?

縮進的正確,我認爲

+1

代碼應該改進的一種方法是正確縮進它。 –

+0

對不起。 – 4words

+0

雖然我們鼓勵對問題進行編輯以提供其他信息,但不允許修改足以使已發佈的答案失效的問題的編輯。事實上,共識是任何具有[編輯特權](http://stackoverflow.com/help/privileges/edit)的用戶都應該恢復這種編輯。發表評論後不久我會這樣做。請注意,這並不妨礙您在發佈任何答案之前完全更改問題。我們很樂意在您的新問題上回答[新問題](http://stackoverflow.com/questions/ask)。 – Makyen

回答

0
int score[12][5]; 
void chk() { 
int score = 0; 
... 

你重新聲明score;所以在chk函數的範圍內,score表示int不是數組也不是指針。因此,

Compilation error: Subscripted value is not an array, pointer or vector.

聲明本地變量隱藏具有相同名稱的任何全局變量。如果你需要這兩個變量在相同範圍內,給他們不同的名字。

相關問題