I am doing a homework assignment that reads in a book. First, a line is read in and a pointer made to point at that line. Then a paragraph function reads in lines and stores their address into a array of pointers. Now, I am on reading a chapter (a paragraph recognized by the next line being broke). It should call get_paragraph() and store the address of paragraphs until it comes to a new chapter.
A new chapter is the only time in the book where the first character in the line is not a space. I think this is were I am having problems in my code. All functions up to this point work. I hope I have provided enough information. The code compiles but core dumps when started.
I am a student and learning so please be kind. Thanks.
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
}
For those who would rather see without comments:
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);
}
Can I suggest that you use
forloops instead ofwhiles? You need to stop if you run out of space, so you might as well use the appropriate construct.I suspect you have a bug in this code:
Firstly, shouldn’t it be
chapter[i]instead ofchapter[0]? You want to know if the pointer atchapter[i]points to a space, not the first pointer inchapter. So this will probably loop indefinitely – hence the need for aforloop, so you don’t just loop forever accidentally.Secondly, you increment
iat the end of the while block, and then again in thechapter[++i]assignment.ihas already been incremented by the final loop execution before the while condition breaks, so it is already the correct position to use.++iincrements before yielding the value, so presumably you meant to havei++here, so that it would increment after yielding the current value ofi. Either way, it’s confusing one of us as to what you mean, so maybe just put the increment on a separate line for clarity. The compiler will sort out any available optimisation.Finally (and I might well be wrong here) why are you setting the value to
'\0'? That’s a null character, isn’t it? But your array is of pointers. The null pointer would be0, rather than'\0', I think. If I’m right, you might have still got away with it if'\0'yields the same set of zeroes as the null pointer.