Tristan Pearce
Tristan Pearce

Reputation: 89

Add node at end of linked list in c

I am attempting to add a node to the end of a linked list. I use a void function and pass my struct into it, but once it has ran through the add function, my struct is still empty. here is the code.

struct part {
    char* name;
    float price;
    int quantity;
    struct part *next;
};

typedef struct part partType;

void addEnd(partType *item) {  
    partType *temp1=NULL, *temp2=NULL;
    char temp[100];  

    temp1 = (struct part *)malloc(sizeof(partType));  

    if (!temp1)
        printf("malloc failed\n");

    temp1->name = malloc(sizeof(char)*100);

    printf("Please enter item name: \n");
    fgets(temp, 100, stdin);     
    strcpy(temp1->name, temp);      

    printf("Please enter item price: \n");
    fgets(temp, 100, stdin);
    sscanf(temp, "%f", &temp1->price);      

    printf("Please enter item quantity: \n");
    fgets(temp, 100, stdin);
    sscanf(temp, "%d", &temp1->quantity);  

    // Copying the Head location into another node.  
    temp2 = item;  

    if (item == NULL) {  
        // If List is empty we create First Node.  
        item = temp1;  
        item->next = NULL;
        printf("%s%.2f\n%d\n", item->name, item->price, item->quantity);  
    } else {  
       // Traverse down to end of the list.  
       while (temp2->next != NULL)  
           temp2 = temp2->next;  

       // Append at the end of the list.  
       temp1->next = NULL;  
       temp2->next = temp1;
       printf("%s%.2f\n%d\n", item->name, item->price, item->quantity);
    }
} 

item is null when it is initially passed into the function, but for some reason comes out null even though i have the if statement that sets item equal to temp1.

Upvotes: 0

Views: 3113

Answers (3)

John Bode
John Bode

Reputation: 123598

You need to modify the value of the pointer, so you need an extra level of indirection:

void addEnd(partType **item)
{
   ...
   temp2 = *item;
   ...  
   if (*item == NULL)
   {
     *item = temp1;
     (*item)->next = NULL;
     printf("%s%.2f\n%d\n", (*item)->name, (*item)->price, (*item)->quantity);
     ...
}

and you would call this as

partType *newItem;
...
addEnd(&newItem);

Upvotes: 3

kailoon
kailoon

Reputation: 2069

This is only a guess as you didn't actually show how you are calling this function. I'm assuming you have a pointer called item somewhere of type part set to NULL. You then call this function with that variable. This doesn't actually mean a pointer to a pointer of that type. It creates a local copy of that pointer variable when you perform that function call, which is currently pointing to NULL. You set the local copy of that item pointer to temp and then that local copy is lost at the end of the function.

Upvotes: 0

Matthias
Matthias

Reputation: 8180

If item is NULL, when you call the function, it is bound to be NULL also after the function. C doesn't know reference parameter, they are "simulated" by pointers. If you want to change a pointer within a function, you need a pointer to a pointer.

Upvotes: 2

Related Questions