List Reading - Printing

This is a discussion on List Reading - Printing within the C Programming forums, part of the General Programming Boards category; I want to make a list reading the input and i want to print the list after filling it. I ...

  1. #1
    ch4
    ch4 is offline
    Newbeer ch4's Avatar
    Join Date
    Jan 2007
    Posts
    142

    List Reading - Printing

    I want to make a list reading the input and i want to print the list after filling it.
    I try hard but i can achieve it because i get faults.

    This is my list
    Code:
    struct AkeraiosStruct {
               int Value;
               AkeraiosStruct * next;
    } AkeraiosStruct;
    
    
    void readinglist (AkeraiosStruct * r){
        int i; 
        
        
           r= malloc (sizeof(struct AkeraiosStruct));
           r->next=NULL;
    
          do{
             i=getchar();
             
             insert_at_end (&r, i);
          
             }while(i!=' ');
    }
    
    
    void insert_at_end (AkeraiosStruct ** ptraddr, int v)
                           /* Insert v as last element of list *ptraddr */
    { while (*ptraddr != NULL)                     /* Go to end of list */
        ptraddr = &((*ptraddr)->next);/* Prepare what we need to change */
      *ptraddr = malloc(sizeof(struct AkeraiosStruct)); /* Space for new node */
      (*ptraddr)->Value = v;                               /* Put value */ 
      (*ptraddr)->next = NULL;              /* There is no next element */
    }
    
    void print(AkeraiosStruct * r)                  /* Print elements of list */
    { while (r != NULL) {       /* Visit list elements up to the end */
        printf("%d --> ", r->Value);         /* Print current element */
        r = r->next;                        /* Go to next element */
      }
      printf("NULL\n");                            /* Print end of list */
    }
    
    int main (void){
        AkeraiosStruct * A;
    
                printf("Give the First number and put space at the end\n\n");
                A=NULL;
                readinglist (A);
                print(A);
    
    return 0;
    }
    I can debugg it.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    Oh then if you can achieve it and if you can debug it, feel free to do so...

    When you say faults, do you mean a segmentation fault or a compiler error ?

  3. #3
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    The first problem is that your readinglist function takes the pointer value, not the pointer itself. This function has no effect on the pointer in main(). If you want to do it this way, you need this:

    Code:
    void readinglist (AkeraiosStruct ** r){
        int i; 
        
        
           *r= malloc (sizeof(struct AkeraiosStruct));
           (*r)->next=NULL;
    
          do{
             i=getchar();
             
             insert_at_end (r, i);
          
             }while(i!=' ');
    }
    And you need call it in main() like so:

    Code:
    readinglist(&A);
    There is no greater sign that a computing technology is worthless than the association of the word "solution" with it.

  4. #4
    ch4
    ch4 is offline
    Newbeer ch4's Avatar
    Join Date
    Jan 2007
    Posts
    142
    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. urgent help please...
    By peter_hii in forum C++ Programming
    Replies: 11
    Last Post: 10-30-2006, 05:37 AM
  2. urgent please please..
    By peter_hii in forum C++ Programming
    Replies: 4
    Last Post: 10-30-2006, 05:35 AM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. Replies: 6
    Last Post: 03-02-2005, 01:45 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

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