Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 01-12-2004, 06:44 PM   #1
Jose Muņiz
Registered: Jul 2003
Location: Mexico City
Distribution: Slackware 9.1, SuSE 9.1
Posts: 248

Rep: Reputation: 32
Linked Lists: Pointers sent to functions as arguments do not change their valur

After long C vacations, I decided to create a linked list to see if I remembered things right. I see I don't. My linked list is supposed to be a queue.

In function mk_item, if tail equals NULL, then it means there is no linked list created. Hence, a list is created and both head and tail are forced to point into that direction. However, when this function exits, the value of head returns to the original NULL. I don't understand why, ,considering it is pointers we are talking about. What I am doing wrong? And how can I solve it? Thank you very much for your help

#include <stdio.h>
#include <stdlib.h>

/* List of structures */
struct list
  int ID;
  int value;
  struct list* pointer;
typedef struct list list;

/* Function Prototypes */
void show_menu(void);
list* mk_item(list*, list*);
list* rm_item(list*, list*);
void print_list(list*);

/* Global variables   */
int ID_assigner = 0;

/* main()              */
int main()
  char option;
  list* tail = NULL;
  list* head = NULL;
   while (option != 'd')
    scanf("%s", &option);

    switch (option)
      case 'a': case 'A':
        tail = mk_item(tail, head);
        printf("The value of head is %p\n" ,head);
      case 'b': case 'B':
        head = rm_item(tail, head);
      case 'c': case 'C':
        printf("%s \t\t %s \n", "ID", "Value");
      case 'd': case 'D':
      default :
        printf("\aOoops! Invalid option!\n");

  printf("You have made a small program very happy :D Thank you for using me. \n");
  return 0;

/* show_menu()          */
void show_menu(void)
  printf("Choose the correct option \n"
         " a)  Create a new item    \n"
         " b)  Delete last item     \n"
         " c)  Print items          \n"
         " d)  Exit                 \n"
         " $ ");

list* mk_item(list* tail, list* head)

  if (tail == NULL)
    tail = (list*) malloc(sizeof(list));
    head = tail;

    tail -> ID = ++ID_assigner;
    tail -> pointer = NULL;

    printf("Please provide the value of item ID %d: ", ID_assigner);
    scanf("%d", &tail -> value);

    printf("Value of tail is %p and value of head is %p\n", tail, head);


    list* temp = NULL;

    temp = (list*) malloc(sizeof(list));

    temp -> ID = ++ID_assigner;
    temp -> pointer = NULL;

    printf("Please provide the value of item ID %d: ", ID_assigner);
    scanf("%d", &temp -> value);

    head -> pointer = temp;
    head = temp;
  return tail;

list* rm_item(list* tail, list* head)
  list* temp = tail;

  if (temp -> pointer == NULL || temp == NULL) 
    tail = NULL;
    while ((temp -> pointer)-> pointer != NULL)
      temp = temp -> pointer;   

    head = temp;
  return head;

void print_list(list* tail)
  if (tail == NULL)
    printf("All printed\n");
    printf("%d \t\t %d \n", tail -> ID, tail -> value);
    print_list(tail -> pointer);
Old 01-12-2004, 07:02 PM   #2
Senior Member
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
I am slightly confused by your logic here. But I did go out with the guys after work today so that might be why.....

The way I do link lists is to declare a structure (I will call it NODE) like the one you have created with some kind of data inside it and a next pointer. I start the program with head and tail (both NODE's) pointing to null. My routine that adds data would first check to see if head is NULL. If it is it creates a new NODE (mallocs data, sets the data value, and sets the next value to NULL) and points head to this node and points tail to head->next. If head isn't null it checks to see if tail is NULL, if it is it creates a new node where tail is currently pointing. If both head and tail aren't null then tail = tail->next and we create a new node where tail is currently pointing. To pull data off the queue we simply save the data from head, and point head to head->next. This of course assumes a FIFO queue.
Old 01-12-2004, 07:18 PM   #3
LQ Guru
Registered: Feb 2003
Location: Colorado Springs, CO
Distribution: Gentoo
Posts: 2,018

Rep: Reputation: 48
I think maybe what you need to do is to pass the pointers to mk_item by reference, rather than by value. The two parameters to mk_item (list * tail, list * head) are local copies that are lost once the function returns, so if you set "tail = " something, it's only for the scope of that function.

So try this:

list * mk_item( list * &tail, list * &head )
Old 01-12-2004, 07:45 PM   #4
Senior Member
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
wapcaplet is right but references are C++ if your doing it in C(seems like you are) you'll have to do it with a double pointer, redeclare mk_item like this

list* mk_item(list* tail, list** head) {

and call it with

mk_item(tail, &head)


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
linked lists: java vs c kpachopoulos Programming 4 10-30-2005 04:41 PM
Linked Lists and Queues Kroenecker Programming 2 04-09-2005 01:59 AM
Linked Lists leonidg Programming 7 03-10-2005 02:07 AM
Linked Lists - What and Why? scuzzman Programming 9 12-31-2004 10:51 AM
c++ linked lists jclark00001 Programming 10 02-23-2003 02:40 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:18 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration