我不知道为什么不工作。我正在尝试打印超过80个字符的行(来自输入)。但我根本没有得到正确的字符。我不知道我做错了什么。没有人在那里知道??
注意:我不希望你给我写这个功能的全新程序(打印行超过80)。我只想知道程序的错误方面。更正
注意:最后一行会覆盖之前的行。
#include <stdio.h>
#define MAXARRAYSIZE 500
char c;
int i = 0;
int j = 0;
int jCopy = 0;
char line[MAXARRAYSIZE];
char linesToPrint[MAXARRAYSIZE][MAXARRAYSIZE];
void emptyArray(char theArray[]);
void InsertArrayIntoArray(char to[MAXARRAYSIZE][MAXARRAYSIZE], char from[], int toIndex, int lengthSoFar);
void printArray(char theArray[MAXARRAYSIZE][MAXARRAYSIZE], int lengthSoFarX);
int main(void) {
while ((c = getchar()) != EOF) {
if (i > MAXARRAYSIZE) {
i = 0;
}
if (j > MAXARRAYSIZE) {
j = 0;
}
if (c != '\n') {
line[i] = c;
i++;
//printf("%d",i);
} else {
printf("\n j:%d \n i:%d", j, i);
j++;
jCopy = j;
if (i >= 10) {
InsertArrayIntoArray(linesToPrint, line, j, i);
//printArray(linesToPrint, j, i);
}
emptyArray(line);
i = 0;
}
}
printArray(linesToPrint, jCopy);
}
void emptyArray(char theArray[]) {
int i;
for (i = 0; i < sizeof(theArray) / sizeof(theArray[0]); i++) {
theArray[i] = 0;
}
}
void InsertArrayIntoArray(char to[MAXARRAYSIZE][MAXARRAYSIZE], char which[], int toSize, int whichSize) {
int i, j;
//printf("\n To size: %d \t Which Size: %d",toSize,whichSize);
for (i = 0; i < toSize; i++) {
for (j = 0; j < whichSize; j++) {
to[i][j] = which[j];
}
}
}
void printArray(char theArray[MAXARRAYSIZE][MAXARRAYSIZE], int lengthSoFarX) {
int i, j;
for (i = 0; i < lengthSoFarX; i++) {
printf("\n Line %d :", i + 1);
printf(" %s\n", theArray[i]);
}
}
答案 0 :(得分:0)
首先不能采用sizeof(theArray)因为theArray是Joachim Pileborg指出的指针。第二个错误是在InsertArrayIntoArray函数中。 它应该是这样的:
void InsertArrayIntoArray(char to[MAXARRAYSIZE][MAXARRAYSIZE], char which[], int toSize, int whichSize) {
int i, j;
//printf("\n To size: %d \t Which Size: %d", toSize, whichSize);
for (j = 0; j < whichSize; j++) {
to[toSize][j] = which[j];
}
}