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

  • SEARCH
  • Home
  • 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 8492033
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:36:09+00:00 2026-06-10T22:36:09+00:00

I’m having some trouble with the following: void BuildList(cs460hwp hw) { FILE* fp; fp

  • 0

I’m having some trouble with the following:

void BuildList(cs460hwp hw)
{

    FILE* fp;
    fp = fopen("HW2input.dat", "r");
    if(fp == NULL)
    {
        printf("Couldn't open the file.");
        return;
    }
    int numStudents;
    int i;
    bool success;
    char* dueDate = malloc(9*sizeof(char));
    char* course = malloc(7*sizeof(char));
    char* wsuid = malloc(9*sizeof(char));
    char* subDate = malloc(9*sizeof(char));
    double points1 = 0;
    double points2 = 0;
    cs460hwp stuInsert = NULL;
    fscanf(fp, "%d", &numStudents);
    fscanf(fp, "%s", dueDate);
    for(i = 0; i < numStudents; i++)
    {
        stuInsert = malloc(sizeof(cs460hwp));
        fscanf(fp, "%s %s %s %lf", course, wsuid, subDate, &points1);
        strcpy(stuInsert->course, course);
        strcpy(stuInsert->wsuid, wsuid);
        strcpy(stuInsert->subdate, subDate);
        stuInsert->points1 = points1;
        stuInsert->points2 = CalculatePoints(dueDate, subDate, points1);
        stuInsert->nextPtr = NULL;
        if(hw == NULL)
        {
            hw = stuInsert;
        } 
        else
        {
            stuInsert->nextPtr = hw;
            hw = stuInsert;
        }
    }
    free(course);
    free(wsuid);
    free(subDate);
    free(dueDate);
    PrintGrades(hw);
    fclose(fp);
}

struct hwpoints
{
    char course[7];
    char wsuid[9];
    char subdate[9];
    double points1;
    double points2;
    struct hwpoints *nextPtr;
};

typedef struct hwpoints *cs460hwp;

My goal here is to insert every entry to the top of the list. However, whenever I try to assign anything to nextPtr (such as in the else clause), it gets filled with garbage values. They’re mostly truncated versions of old data, which leads me to believe they’re being taken from the heap. I’ve been reading (a lot), but I’m having trouble finding advice on this particular problem.

nextPtr always becomes junk, and nextPtr->nextPtr causes a segfault. For every iteration of the loop. hw remains fine, but its pointer value never gets updated properly.

Even when I’ve attempted to move the memory allocation for the struct into a function, I’ve had the same (or similar) issues.

Can anyone point me in the right direction?

  • 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-06-10T22:36:11+00:00Added an answer on June 10, 2026 at 10:36 pm

    Two problems.

    1) As pb2q mentioned, you are passing a pointer to a struct and trying to assign what the arg points to. That’s allowed by the compiler, but it doesn’t do anything for you outside the function. It might be OK in your case if:

    void main()
    {
       cs460hwp hw = NULL;
       BuildList(hw);
       return;
    }
    

    Is the whole of your function. I don’t know the assignment so you need to figure out if that’s acceptable to you or not.

    2) The much bigger problem:

    stuInsert = malloc(sizeof(cs460hwp)); 
    

    Did you check what sizeof(cs460hwp) comes out to be? it’s 4. You’re allocating enough memory for the size of a pointer, not the size of your structure. I’m pretty sure this is not what you want to do and this is what is killing you. Just for kicks, replace it with malloc(100) and see if your problem goes away. If so you just need to figure out what size you really want. 😉

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
In my XML file chapters tag has more chapter tag.i need to display chapters
I am trying to render a haml file in a javascript response like so:

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.