4

Something in the while loop is giving me this error. I can't figure out what to look up because this error seems way to common to find out what to do with my specific example

#include <stdlib.h>
#include <stdio.h>
/*Structure declarations*/
struct Data {
    int id_number;
    float ratings;
};
typedef struct Node{
    struct Data player;
    struct Node *next;
}Node;

/*function declaration*/
void push(struct Node *list_head, int unique_id, float grade);
int main(){
    /* Initialize list_head to Null since list is empty at first */
    Node *list_head = NULL;     
    Node *traversalPtr;

    push(list_head, 1, 4.0);
    push(list_head, 2, 3.87);
    push(list_head, 3, 3.60);

    traversalPtr = list_head;
    while(traversalPtr -> next != NULL){
        printf("%d\n",traversalPtr -> player.id_number);
        traversalPtr = traversalPtr -> next;
    }   
}

...function declarations
6
  • 1
    You need to pass the address of list head to push(). Commented Sep 30, 2019 at 14:49
  • Welcome! Please include the definition of push in your question. As @VladfromMoscow says, you have a problem with your push function declaration/definition. Commented Sep 30, 2019 at 14:51
  • Just to point out, push is not necessarily calling itself because the definition of push has not been provided, just the declaration. Commented Sep 30, 2019 at 14:52
  • We really need the definition of push() to conclusively answer this. @CodakBlack, would it be possible to edit this into your question? Commented Sep 30, 2019 at 14:55
  • If you already run your program in a debugger and get this error, you should see exactly where you got that error. There you can inspect your variables and check for expected vs real values. Commented Sep 30, 2019 at 16:00

1 Answer 1

4

The problem is that the function

void push(struct Node *list_head, int unique_id, float grade);

deals with copies of original pointers defined in main because the pointers are passed by values.

You should declare the function like

void push(struct Node **list_head, int unique_id, float grade);

and call it like

push( &list_head, 1, 4.0 );

Here is an example of how the function can be defined (I assume that the function appends nodes to its tail).

int push(struct Node **list_head, int unique_id, float grade)
{
    struct Node *node = malloc( sizeof( struct Node ) );
    int success = node != NULL;

    if ( success )
    {
        node->player.id_number = unique_id;
        node->player.ratings   = grade; 
        node->next = NULL;

        while ( *list_head ) list_head = &( *list_head )->next;

        *list_head = node;
    }

    return success; 
}

Also this loop

traversalPtr = list_head;
while(traversalPtr -> next != NULL){
    printf("%d\n",traversalPtr -> player.id_number);
    traversalPtr = traversalPtr -> next;
}   

is incorrect. It should look like

traversalPtr = list_head;
while(traversalPtr != NULL){
    printf("%d\n",traversalPtr -> player.id_number);
    traversalPtr = traversalPtr -> next;
}   
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.