2013-05-07 124 views
0

掃描arr函數問題。二維陣列掃描崩潰

void scan_arr(double ar[3][5]) // Declares that it is a 3 x 5 
{ 

    int x; 
    int y; 
    printf("Enter arrays of 3x5\n"); 

    for(x = 0; x < 3; x++) // Shows that this loop shall be done 3 times 
    { 
     for(y = 0; y < 5; y++) // Shows that 5 times * the number of the first loop 
     { 
      scanf("%lf",ar[x][y]); // Scans @ x * y and terminates after first input 
     } 
    } 
} 

回答

4

這是因爲你缺少的ar[x][y]在前面的符號:

scanf("%lf", &ar[x][y]); 
//   ^
//   | 
//   Here 

scanf預計其中值是要存儲的項的地址,所以你需要使用「取地址「運營商&

+0

Riiiight!我覺得很愚蠢:)。那麼我想每個人都可以在編程學習中犯這樣一個簡單的錯誤。 – user2354897 2013-05-07 18:55:04

+0

@ user2354897當然,這是一個非常常見的錯誤。即使是有經驗的程序員也會時不時的,因爲編譯器無法檢測到它。如果修復程序適用於您,請考慮通過單擊旁邊的複選標記來接受答案。這會讓其他人知道你不再需要積極尋找改進的答案,並在堆棧溢出中贏得徽章。 – dasblinkenlight 2013-05-07 18:58:07

1

您需要通過scanf功能的數組元素的地址,所以替換此:

scanf("%lf",ar[x][y]); 

scanf("%lf", &ar[x][y]);