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 6773039
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:37:10+00:00 2026-05-26T15:37:10+00:00

I am having a problem with my linked list. I’m pretty sure it is

  • 0

I am having a problem with my linked list. I’m pretty sure it is my pointers being off, or I didn’t pass a pointer in the correct way as I am new to c. Structs are also new to me, and c++ is the language I am used to and there are more differences than I was aware of. I could do this program in c++ in no time but anyway here is my code.

void add_process(struct process new_process, struct process *head, struct process *current){

    new_process.next = NULL;

    if(head == NULL){
        head = &new_process;
        current = head;
        head->next = NULL;
    }
    else if(new_process.timeNeeded < head->timeNeeded){
        temp = head->next;
        head = &new_process;
        new_process.next = temp;
    }
    else{
        current = head;
        while(new_process.timeNeeded > current->timeNeeded){
            temp = current;
            current = current->next;
        }
        temp->next = &new_process;
        new_process.next = current;
    }
}

I am reading values from a file into the process, the only one I am currently using is timeNeeded which is an int. And I am trying to order the list by shortest timeNeeded first.

int main(){
    FILE *readfile;
    readfile = fopen("data.txt","r");


    head = NULL;
    current = NULL;

    while(fscanf(readfile, "%s %i %i %i", 
        &new_process.processName, &new_process.arrivalTime, 
            &new_process.timeNeeded, &new_process.priority) != EOF)  {

                add_process(new_process, head, current);
      }
    current = head;

    while(current->next != NULL){
        printf("%s %i %i %i\n", new_process.processName, new_process.arrivalTime, new_process.timeNeeded, new_process.priority);
        current = current->next;
    }

    return 0;
}

The program crashes at the print which is not the problem. The first problem is that it my program enters the if(head==NULL) loop everytime and inserts there. So head is probably never being changed but I am not sure how to fix that, I’m pretty sure its a double pointer but not positive. And I am also am sure there are other problems so if you could point me into the correct direction, and if I’m doing anything completely wrong let me know.

EDIT: OK so after adding the pointer to head I get an error at head->next = NULL saying “expression must have pointer-to-class type.” Tried adding a * before head but didn’t seem to help. Anyone know how to fix it?

  • 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-26T15:37:11+00:00Added an answer on May 26, 2026 at 3:37 pm

    Your add_process function here:

    void add_process(struct process new_process, 
                     struct process *head, 
                     struct process *current)
    

    Takes whatever pointer you pass into it by-value. That means after your call in the while loop here:

    while(fscanf(readfile, "%s %i %i %i", 
        &new_process.processName, &new_process.arrivalTime, 
        &new_process.timeNeeded, &new_process.priority) != EOF)  
    {
    
            add_process(new_process, head, current);
    }
    

    head will still be NULL because it never got changed. To have it actually change head pointer and not some other pointer modify your add_process to take a double level pointer:

    void add_process(struct process new_process, 
                     struct process **head, 
                     struct process *current)
    

    Another problem with your above code is that the new_process argument is taken by value as well. So this is a temporary copy of whatever process you passed in. Once add_process returns, new_process goes out of scope. Now that means you have a dangling pointer in your linked list pointing to invalid memory.

    To fix this you should use malloc to dynamically allocate the memory and then make a copy of new_process. Then have your linked list point to the malloc‘ed process. Objects created on the heap with malloc will persist until it’s freed.

    Here’s a quick example to give you an idea:

    typedef struct process Process;
    void add_process(Process new_process, Process **head, Process *current)
    {
        Process *new_proc_copy = (Process *)malloc( sizeof(Process) );
        // now copy over the stuff from 
        // new_process over to this one
        memcpy((char *)new_proc_copy, (char *)new_proc, sizeof(Process));
    
        if(*head == NULL)
        {
            *head = new_process_copy;
            current = *head;
            (*head)->next = NULL;
        }
        else if(new_process.timeNeeded < head->timeNeeded)
        {
            // handle this case
        }
        else
        {
            // handle rest of your stuff
        }
    }
    

    Don’t forget to free the malloc’ed memory when done. This is best done in your process cleanup function — the destructor equivalent in C++ only you have to call it manually.

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

Sidebar

Related Questions

I'm currently having an issue with linked list and pointers in C. The problem
I am reading XOR linked list (from Wikipedia).But I am having some problems in
I'm having trouble reversing my doublely linked deque list (with only a back sentinel)
I got a problem here, implementing a Sorted Doubly Linked List of first and
I'm working on a problem which requires me to use the STL linked list
I am currently working on a stack that implements linked list. I am having
I am having some trouble running this linked list implementation (containing words as data).
I am trying to sort a linked list. I am having problems with const.
Having problem with the middle Div not expanding to the width http://acs.graphicsmayhem.com/images/middiv.jpg Ok, how
We're having problem with a huge number of legacy stored procedures at work. Do

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.