2016-01-23 37 views
-1

這是我得到的錯誤:錯誤:預期表達式之前 '=' 令牌

c:17:15: error: expected expression before ‘=’ token 
      suma + = mat[N][M]; 

這是我的代碼:

#include<stdio.h> 
#define N 3 
#define M 3 

int mat[N][M]i,j,k,l,m,n; 
int vector[N]; 
int suma; 

int main (void){ 
    for(i=0;i<N;i++){ 
    for(j=0;j<M;j++){ 
     printf("Enter a number: "); 
     scanf("%d", &mat[N][M]); 
    } 
    } 
    for(k=0;k<N;k++){ 
     suma=0; 
    for(l=0;l<M;l++){ 
     **suma+ = mat[N][M]; 
    } 
    vector[i]=suma; 
    } 
    return 0; 
} 

回答

1
+= != + = 

含義:只寫:suma += mat[N][M];
............................................^no space。

爲了完整起見,我將添加什麼Sourav提到: 您需要先for循環之後ii=0)復位,避免邊界內存的訪問並導致未定義的行爲。

+2

它的工作原理!謝謝! –

+1

你錯過了索引部分。 :) –

+0

你是對的,雖然tbh我沒有找到,因爲問題似乎很明確:) – Idos

0

要回答編譯器問題,+=+ =不是同一回事。您應該使用+=,而不要在兩者之間留出空間。

我相信**suma排字錯誤在您的代碼。 (或錯過格式化)。你的程序調用undefined behavior。在後面的循環中,

vector[i]=suma; 

您沒有重置i。它會嘗試訪問超出限制的內存。

要麼你需要使用k作爲索引,或者重置i並根據需要增加。

+0

好的,也謝謝:) –

+0

你參考放**矢量[k] + = suma; ** ?? –

+0

@ V_WraMphT3R nope,'vector [k] = suma;' –

相關問題