Thread: print linked list problem

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    4

    print linked list problem

    Code:
    #include<stdio.h>#include<stdlib.h>
    #include<string.h>
    #include<stdio.h>
    #include<stdlib.h>
    
    
    struct StudentRecord {
    char *courseName;
    float Mark;
    struct StudentRecord *next;
    };
    
    
    typedef struct StudentRecord *StudentRecordType;
    
    
    typedef struct {
    char *firstName;
    char *lastName;
    int idNumber;
    StudentRecordType courses;
    } StudentType;
    
    
    
    
    
    
    struct classNodeStruct {
    StudentType *aStudent;
    struct classNodeStruct *next;
    };
    typedef struct classNodeStruct *ClassNodeType;
    
    
    
    
    main()
    {  
          ClassNodeType arxeio,first;
          char *id;
          FILE *pfile;
          char *name;
          char *lastname;
          char *age,*number;
          int intnumber,i;
          
          
          
          pfile=fopen("data.txt","r");
          
          if (pfile!=NULL)
          {
                          
          number=(char *)malloc(42*sizeof(char));
          fscanf(pfile,"%s",number);
          intnumber=atoi(number);
          first=NULL;        
          for (i=0; i<intnumber; i++)
          {    
            arxeio=(struct classNodeStruct *)malloc(sizeof(struct classNodeStruct));
            arxeio->aStudent=(StudentType *)malloc(sizeof(StudentType));
            arxeio->aStudent->courses=(struct StudentRecord *)malloc(sizeof(struct StudentRecord));
            arxeio->aStudent->firstName=(char *)malloc(42*sizeof(char));
            arxeio->aStudent->lastName=(char *)malloc(42*sizeof(char));
            id=(char *)malloc(42*sizeof(char));
         
            fscanf(pfile,"%s %s %s",arxeio->aStudent->firstName,arxeio->aStudent->lastName,id);
            arxeio->aStudent->idNumber=atoi(id);
          
            arxeio->next=first;
            first=arxeio;
          }
          for (arxeio=first; arxeio=NULL; arxeio->next=arxeio)
          {
              printf("\n %s \n %s \n %d \n ",arxeio->aStudent->firstName,arxeio->aStudent->lastName,arxeio->aStudent->idNumber);
             } 
            fclose(pfile);
          
          }
          else
          {
            printf("could not find the file");
          }
          getchar();
          }
    the file data.txt contains the following text
    "3
    giannis dimitrakopoulos 14445
    dimitris antonakis 12245
    giannis kamenos 4598"

    Why i cant print my list?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Here's what I get when I compile your code:
    Code:
    $ make foo
    gcc -g -Wall -std=c99 -o foo foo.c -lpthread -lm -lpq
    foo.c:39:1: warning: return type defaults to ‘int’ [enabled by default]
    foo.c: In function ‘main’:
    foo.c:75:9: warning: suggest parentheses around assignment used as truth value [-Wparentheses]
    foo.c:46:11: warning: unused variable ‘age’ [-Wunused-variable]
    foo.c:45:11: warning: unused variable ‘lastname’ [-Wunused-variable]
    foo.c:44:11: warning: unused variable ‘name’ [-Wunused-variable]
    Those first two need to be addressed (details below).

    Before we get to the (probable) root of your issue, a couple things.

    First, don't typedef a pointer. All it does is make it confusing to keep track of what needs to be dereferenced, what needs the address-of operator, and just how many levels of indirection there are at any point. Besides, you picked really confusing names that don't indicate there is a pointer. ClassNodeType is not clear at all. ClassNodePointer would be better, but best of all, don't typedef the pointers. EDIT: To appease those sticklers for detail, there are perfectly good times to typedef a pointer, in particular when making an opaque data structure for a library. Your program is not such a case.

    Second, what the hell is up with 42 everywhere. Yes, I've read hitchhiker's guide, but sprinkling your code with magic numbers like that is just ridiculous. #define a constant and use that. But better still, just use char arrays. There is no reason you should be malloc'ing char arrays there, since you force them all to length 42, and never free them.

    Third, don't cast malloc, it's not necessary and it can hide errors (if the compiler complains, you're compiling as C++, not C). Read this link: FAQ > Casting malloc - Cprogramming.com.

    Fourth, if you are fscanf'ing into a string, then using atoi, just fscanf "%d" instead of "%s", it does the conversion for you.

    On line 39, the correct form is "int main(void)" and return an int at the bottom of main, usually 0. Read this link.

    On line 75, there's a difference between = (assignment) and == (comparison). Look:
    Code:
    for (arxeio=first; arxeio=NULL; arxeio->next=arxeio)
    That single = assigns NULL to arxeio. That value (NULL) is then used to check if your loop should continue. Since NULL acts like 0, it evaluates to false and your loop never runs.

    Maybe there are more issues, but that's all I got at first glance. It should keep you busy for a while.
    Last edited by anduril462; 05-28-2012 at 10:45 PM.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    4
    Unfortunatelly typedef,ClassNodeType is part of the exercise,i dont see any reason either to make things complicated!I write code with devC++ beacuse this is the program we use in my class,malloc without cast will cause a compile error! (When i replaced "arxeio=NULL" with "arxeio!=NULL" the program prints "giannis kamenos 4598 giannis kamenos 4598 giannis kamenos 4598 giannis kamenos 4598 giannis kamenos 4598 giannis kamenos 4598....." and never come to an end!

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by giannis1990
    malloc without cast will cause a compile error!
    That indicates that you did not configure your compiler to compile your code as C, and it defaulted to compiling as C++. One thing to do is to check that your source files are named with a ".c" extension.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    1
    giannis1990, can you send me the pronunciation of exercise (in any language) and the code when you finish?

  6. #6
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    >giannis1990, can you send me the pronunciation of exercise (in any language) and the code when you finish?
    Are we sneaking your class mates work!!
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by giannis1990 View Post
    Unfortunatelly typedef,ClassNodeType is part of the exercise,i dont see any reason either to make things complicated!I write code with devC++ beacuse this is the program we use in my class,malloc without cast will cause a compile error! (When i replaced "arxeio=NULL" with "arxeio!=NULL" the program prints "giannis kamenos 4598 giannis kamenos 4598 giannis kamenos 4598 giannis kamenos 4598 giannis kamenos 4598 giannis kamenos 4598....." and never come to an end!
    Something is wrong here:
    Code:
    for (arxeio=first; arxeio=NULL; arxeio->next=arxeio)

  8. #8
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Does not the test condition look wrong also?
    I would guess "==" or "!=" instead of "=".

    Tim S.

    Code:
    for (arxeio=first; arxeio=NULL; arxeio->next=arxeio)
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  9. #9
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by stahta01 View Post
    Does not the test condition look wrong also?
    I would guess "==" or "!=" instead of "=".

    Tim S.

    Code:
    for (arxeio=first; arxeio=NULL; arxeio->next=arxeio)
    Yeah, that was wrong too, though I mentioned it in an earlier post and the OP seems to have fixed that (see post #3). I just copy-pasted that from the original code and forgot to change it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to print the values of a part of a linked list
    By sharrakor in forum C Programming
    Replies: 3
    Last Post: 02-05-2009, 08:14 AM
  2. i want to print my linked list in the reverse order please help
    By raghu_equinox in forum C Programming
    Replies: 9
    Last Post: 10-14-2006, 12:45 AM
  3. print a text-file in linked list
    By tidemann in forum C Programming
    Replies: 10
    Last Post: 09-19-2006, 05:22 AM
  4. Replies: 13
    Last Post: 05-08-2006, 11:14 AM
  5. Linked List print out
    By axon in forum C++ Programming
    Replies: 6
    Last Post: 10-19-2003, 07:15 PM