C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 02-21-2003, 10:27 AM   #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;
}
bsimbeck is offline   Reply With Quote
Old 02-21-2003, 10:42 AM   #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);
Monster is offline   Reply With Quote
Old 02-21-2003, 11:05 AM   #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
Monster is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 09:21 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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