我正在做一個閱讀書籍的家庭作業。首先讀入一行,並在該行指向一個指針。然後一個段落函數讀取行並將它們的地址存儲到一個指針數組中。現在,我正在閱讀一章(下一行被識破的一段被破壞)。它應該調用get_paragraph()
並存儲段落的地址,直到出現新章節。解析章節時的Coredump
新的篇章是在本書,在該行的第一個字符是不是空格唯一的一次。我認爲這是我的代碼有問題。到此爲止的所有功能都能正常工作。我希望我已經提供了足夠的信息。代碼在啓動時編譯但核心轉儲。
我是一名學生,學習的,所以請善待。謝謝。
char*** get_chapter(FILE * infile){
int i=0;
char **chapter[10000];//an array of pointers
// Populate the array
while(chapter[i]=get_paragraph(infile)) { //get address store into array
if(!isspace(**chapter[0])){ //check to see if it is a new chapter<---problem line?
// save paragraph not used in chapter using static to put into next chapter
break;
}
i++;//increment array
}
//add the null
chapter[++i]='\0';//put a null at the end to signify end of array
//Malloc the pointer
char**(*chap) = malloc(i * sizeof(*chap));//malloc space
//Copy the array to the pointer
i=0;//reset address
while(chapter[i]){//while there are addresses in chapter
chap[i] = chapter[i++];//change addresses into chap
}
chap[i]='\0';//null to signify end of chapter
//Return the pointer
return(chap);//return pointer to array
}
對於那些誰寧願看到沒有評論:
char*** get_chapter(FILE * infile){
int i=0;
char **chapter[10000];
while(chapter[i]=get_paragraph(infile)) {
if(!isspace(**chapter[0])){
break;
}
i++;
}
chapter[++i]='\0';
char**(*chap) = malloc(i * sizeof(*chap));//malloc space
i=0;
while(chapter[i]){
chap[i] = chapter[i++];
}
chap[i]='\0';
return(chap);
}
我不知道爲什麼有人需要看到你的代碼*沒有*評論。我們需要看到的是格式正確的代碼。粘貼後,使用1010按鈕將整個代碼塊縮進四個空格。然後修復縮進的其餘部分,這樣纔有意義。如果你在一個只使用空格字符**的編輯器中縮進它,那麼在將它粘貼到SO之前,然後使用1010按鈕將其格式化爲代碼,這將更容易。 – RBerteig 2010-11-11 06:43:43
謝謝,在我的代碼中,我的代碼中實際上有一個#define CHAPTERLIM,但通過這個數字,所以不必解釋它,但我現在編輯了這篇文章。所以它看起來像char **章節[CHAPTERLIM]; – pisfire 2010-11-11 06:45:12
這是我在一段時間裏看到的更好的作業問題之一。我讓你的第一段更容易閱讀。 – 2010-11-11 06:49:11