我正在爲我正在進行的C編程課程的實驗任務工作。我在我的本地Cygwin目錄中編寫了代碼,使用gcc
對其進行編譯,生成的可執行文件完全按照我希望的方式工作,沒有任何錯誤。爲什麼gcc不能一致地編譯這段代碼?
當我將我的代碼複製到我的學校的UNIX服務器上並用gcc
進行編譯時,我沒有收到任何錯誤,但是當我嘗試運行它時,沒有任何反應。
我試圖這樣做gcc 2darray.c -Wall -pedantic
,這是返回什麼:
2darray.c: In function 'main':
2darray.c:5:3: warning: missing braces around initializer [-Wmissing-braces]
2darray.c:5:3: warning: (near initialization for 'M[0]') [-Wmissing-braces]
2darray.c:5:24: warning: C++ style comments are not allowed in ISO C90 [enabled by default]
2darray.c:5:24: warning: (this will be reported only once per input file) [enabled by default]
的錯誤提一些關於初始化數組M
,但我沒有看到我初始化的方式出現任何問題。下面的代碼我試圖編譯:
#include <stdio.h>
int main(void)
{
int M[10][10] = {0}; // creating a 10x10 array and initializing it to 0
int i, j; // loop variables
int sum[10] = {0}; // creating an array to hold the sums of each column of 2d array M
for (i = 1; i < 10; i++) // assigning values to array M as specified in directions
{
for (j = i - 1; j < i; j++)
{
M[i][j] = -i;
M[i][j+1] = i;
M[i][j+2] = -i;
}
}
for (i = 0; i < 10; i++) // printing array M
{
for(j = 0; j < 10; j++)
{
printf("%3d", M[i][j]);
}
printf("\n");
}
printf("\n");
for (i = 0; i < 10; i++) // calculating sum of each column
{
for (j = 0; j < 10; j++)
{
sum[i] = M[j][i] + sum[i];
}
printf("%3d", sum[i]); // printing array sum
}
return 0;
}
我試圖插入變量聲明和第一個for循環和打印語句之間的printf語句,所以也許不順心的事在我的循環?
如果相關,這裏的輸出看起來像從我的Cygwin的目錄,它應該喜歡什麼我在學校的UNIX目錄:
0 0 0 0 0 0 0 0 0 0
-1 1 -1 0 0 0 0 0 0 0
0 -2 2 -2 0 0 0 0 0 0
0 0 -3 3 -3 0 0 0 0 0
0 0 0 -4 4 -4 0 0 0 0
0 0 0 0 -5 5 -5 0 0 0
0 0 0 0 0 -6 6 -6 0 0
0 0 0 0 0 0 -7 7 -7 0
0 0 0 0 0 0 0 -8 8 -8
0 0 0 0 0 0 0 0 -9 9
-1 -1 -2 -3 -4 -5 -6 -7 -8 1
嘗試更改'int M [10] [10] = {0};'爲'int M [10] [10] = {{0}};' – user1969104
感謝您的建議。它雖然沒有修復它。 – user10721
我只是試圖解決警告。我仍然試圖理解你的問題。你可以更好地解釋'可執行文件不起作用'的含義。 – user1969104