我正在做一个阅读书籍的家庭作业。首先读入一行,并在该行指向一个指针。然后一个段落函数读取行并将它们的地址存储到一个指针数组中。现在,我正在阅读一章(下一行被识破的一段被破坏)。它应该调用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