2
嘿,我試圖將一個矩陣存儲在一個字符數組中,然後將其打印出來。C在字符陣列中存儲矩陣和打印
我的代碼,我已經寫了:
#include<stdio.h>
#include<stdlib.h>
int main() {
int i;
int j;
int row=0;
int col=0;
int temp=0;
char c;
int array[3][2] = {{}};
while((c=getchar()) !=EOF && c!=10){
if((c==getchar()) == '\n'){
array[col++][row];
break;
}
array[col][row++]=c;
}
for(i=0; i<=2; i++){
for(j=0; j<=3; j++){
printf("%c ", array[i][j]);
}
printf("\n");
}
}
使用文本文件如:
1 2 3 4
5 6 7 8
9 1 2 3
我想能夠打印回給用戶,但是就是我代碼輸出是:
1 2 3 4
3 4 5 6
5 6 7 8
我無法弄清楚我的代碼有什麼問題,有些如何關閉一個迭代我的循環,或者與不正確處理新行有關。謝謝!
char數組在哪裏?爲什麼'char c'?爲什麼不''int c'? AFAIK空初始值設定項('int array [3] [2] = {{}};')不允許在C中。 – haccks
你能向我解釋我的代碼幾乎工作嗎?另外,當我更改爲int c,並打印出整數而不是字符時,它是打印地址數字我相信,而不是存儲在每個數組中的元素。 – Blued00d
應該是'array [3] [4]' – user3386109