Thread: list printing

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    67

    list printing

    i'm trying to print out elements in a linked list however it wont compile, apparantly next is undeclared, but i have declared it, what is wrong?
    Code:
     
    
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct L {
      int      number;
      struct L *next;
    } listelem;
    
    
    listelem *insert_list(int n, listelem *p) {
    
      listelem *temp = calloc(1,sizeof(listelem));
      temp->number = n;
      temp->next = p;
      return temp;
    }
    
    
    listelem *read_list( void ) {
      int n;
      listelem *lp = NULL;
    
      scanf("%d", &n);
      while (n != 0) {
        lp = insert_list(n,lp);
        scanf("%d", &n);
      }
      printf("\n");
      return lp;
    }
    
    
    int main( void ) {
      listelem *lp = read_list();
      listelem *lp next;
      for( ; lp ; lp = next ) {
        printf( "%c", lp->number );
        next = lp->next;
        free( lp );
        }
    
    
      return 0;
    }
    any help is appreciated

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    for( ; lp ; lp = next ) {
    next isn't declared. next isn't a stand alone variable. It's a member of a structure. As such, the actual variable is lp, and next is a member of lp.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    8
    Code:
     
    int main( void ) {
      listelem *lp = read_list();
      listelem *lp next;                <---------- ?
    If you had read the error message the compiler gave you, and gone to the line number it gave you, this should have been pretty obvious(?).

  4. #4
    Registered User
    Join Date
    Nov 2004
    Posts
    67
    so how do i need to adjust it?

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    67
    ive sorted my problem out now, i was being silly....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM
  2. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  3. urgent please please..
    By peter_hii in forum C++ Programming
    Replies: 4
    Last Post: 10-30-2006, 06:35 AM
  4. Problem with linked list ADT and incomplete structure
    By prawntoast in forum C Programming
    Replies: 1
    Last Post: 04-30-2005, 01:29 AM
  5. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM