Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3391342
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T03:47:36+00:00 2026-05-18T03:47:36+00:00

I am doing a homework assignment that reads in a book. First, a line

  • 0

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);
  }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T03:47:36+00:00Added an answer on May 18, 2026 at 3:47 am

    Can I suggest that you use for loops instead of whiles? 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:

    while(chapter[i]=get_paragraph(infile)) { 
      if(!isspace(**chapter[0])){   
        break;
      }
      i++;
    }
    chapter[++i]='\0';
    

    Firstly, shouldn’t it be chapter[i] instead of chapter[0]? You want to know if the pointer at chapter[i] points to a space, not the first pointer in chapter. So this will probably loop indefinitely – hence the need for a for loop, so you don’t just loop forever accidentally.

    Secondly, you increment i at the end of the while block, and then again in the chapter[++i] assignment. i has already been incremented by the final loop execution before the while condition breaks, so it is already the correct position to use. ++i increments before yielding the value, so presumably you meant to have i++ here, so that it would increment after yielding the current value of i. 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 be 0, 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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm doing a basic homework assignment which looks like this: While input <> -1
This one is a case of not doing your homework.:-) Apart from dynamic loading
Doing odd/even styling with jQuery is pretty easy: $(function() { $(.oddeven tbody tr:odd).addClass(odd); $(.oddeven
When doing TDD , how to tell that's enough tests for this class /
I doing a function in Javascript like the VisualBasic DateDiff. You give two dates
When doing a simple performance measurement, I was astonished to see that calling String.IndexOf(char)
A recent homework assignment I have received asks us to take expressions which could
For a homework assignment in linear algebra, I have solved the following equation using
I have this school assignment that I've been working on and am totally stumped
Some background information, for a homework assignment I had to write a polish notation

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.