Problem with pointers and linked lists

This is a discussion on Problem with pointers and linked lists within the C Programming forums, part of the General Programming Boards category; I hope someone can help me. I am in the process of working on a linked list project. When I ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    6

    Problem with pointers and linked lists

    I hope someone can help me. I am in the process of working on a linked list project. When I do everything in main (commented out right now) it works nodes are created etc... However when I do it in a function it doesn't work, I get a core dump. Am I doing something wrong with passing the pointers as parameters??

    Thanks in advance,

    Branden

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct node{
       int number;
       struct node *next;
       struct node *prev;
    } NODE;
    
    void createlist(NODE *head, NODE *current, int number)
    {
       head = (NODE*)malloc(sizeof(NODE));
       current = head;
       current->number = 15;
       current->next = current;
       current->prev = current;
    }
    
    int main()
    {
       NODE *head = NULL;
       NODE *current = NULL;
    
       //create the first node
       //head = malloc(sizeof(NODE));
       //current = head;
       //current->number = 15;
       //current->next = current;
       //current->prev = current;
       //end first node creation
      
     createlist(head, current, 15);
       printf("%i\n", current->number);
       return 0;
    }

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    You need to pass the address of the pointer to make it work. This is because you change the pointer with the malloc function:
    Code:
    void createlist(NODE **head, NODE **current, int number)
    {
       *head = (NODE*)malloc(sizeof(NODE));
       *current = *head;
       (*current)->number = 15;
       (*current)->next = *current;
       (*current)->prev = *current;
    }
    
    createlist(&head, &current, 15);

  3. #3
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Some explanation:

    In main head is a pointer to NULL.
    When calling createlist a copy of the pointer is placed on the stack (it's also pointing to NULL).
    In the createlist function memory is allocated and the copy of head is pointing to the allocated memory.
    The original head pointer in main is still pointing to NULL.

    If you pass the address of the head pointer, the createlist function has access to the original pointer by dereferencing (place * before the variable), so the allocated memory can be assigned to the original head pointer in the main function.

    Same story for the "current" variable

    Some more info

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointers to pointers in linked lists
    By G4B3 in forum C++ Programming
    Replies: 2
    Last Post: 07-23-2008, 04:54 AM
  2. pointers and linked lists in place of arrays
    By kordric in forum C++ Programming
    Replies: 8
    Last Post: 05-14-2008, 11:59 AM
  3. Double pointers and linked lists.
    By simo_r in forum C Programming
    Replies: 2
    Last Post: 05-06-2008, 05:25 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Linked Lists & Pointers
    By fkheng in forum C Programming
    Replies: 4
    Last Post: 06-10-2003, 08:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21