我寫了一個小程序來組合兩個2d數組。下面是代碼:連接兩個2d char數組
#define MAX 7
int main(void) {
int i, j;
char *array1[] = {"Welt,", "bist", "du"};
char *array2[] = {"noch", "zu", "retten?"};
char final[MAX][MAX];
for(i = 0; i < 3; i++) {
// initialize ith names element with first name
strcpy(final[i], array1[i]);
}
for(j = 0; j < 3; j++) {
// concatenate the last name to the firstname+space string
strcat(final[i], array2[j]);
}
for (i = 0; i != 6; i++) {
printf("%s", final[i]);
}
return EXIT_SUCCESS;
}
我得到這樣真的很奇怪輸出:
世界報,bistbistdunochzuretten uretten恩???
而我想是這樣的:
世界報,bistdunochzuretten
正如你可以看到它是不是完全錯誤的。詞語之間不應有空格。
我該如何修復我的代碼?
sry不,不起作用,或者我無法處理它。 – WirJun 2014-12-07 18:32:27
對不起,我沒有說過,但現在嘗試包括我在代碼中的庫。 – OiciTrap 2014-12-07 18:38:07
很高興我能幫助你。 – OiciTrap 2014-12-07 18:50:06