Thread: Struct/Linked List Problems

  1. #1
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507

    Struct/Linked List Problems

    Taken from the advice on these boards I have been trying to do college assignments off of google to try and further my hobby. When making a linked list I am having trouble inserting data into my structure. I know that scanf needs its values to be passed by reference in order to modify them, however I am unsure of the syntax. I will link the relevant parts and comment the errors.
    The structure
    Code:
    struct student {
           char * firstName;
           char * lastName;
           int code;
           char grade;
           struct student * nextPtr;
           };
    Implementation:
    Code:
    void insert(struct student ** theList)
    {
         
         struct student * newEntry = (struct student *)malloc(sizeof(struct student));
         
         printf("First Name: ");
         scanf("%s", newEntry->firstName);
         printf("Last Name: ");
         scanf("%s", newEntry->lastName);
         printf("Grade: ");
         scanf("%c", newEntry->&grade); //Error
         printf("Course Code: ");
         scanf("%d", newEntry->&code); //Error
         
         if(*theList == NULL)
         *theList = newEntry;
    }
    Im not sure of the syntax to pass the memory address of an item in structure pointer. Thanks again.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You have to allocate memory for your char *'s. You can't just read in data to where they point. They point to arbitrary locations.

  3. #3
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Ugh...dumb thinking on my part...again. You can only do it the way I do it if you use string literals right? As for the grade and course code...how do I enter that through scanf? What is the proper notation for accessing a structure pointer elements address?

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I believe it would be &newEntry->grade.

  5. #5
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Hmm this seems to not be working. Also for some reason it will take the first name and last name line by line...then it gets to the grade/course code part and skips input for the grade and goes right to the course code. I enter that and printf the results back and get garbage.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    That's the problem with scanf(), it will quit or "skip" unexpectedly. fgets() is the preferred method of getting input, but back to scanf():

    You need to add a getchar() (I like doing it immediately after the scanf(), but anyplace before the next scanf() will do), to get rid of the newline char which is still in the input buffer. Otherwise, the next call to scanf() will see that newline and say "Thanks a lot!", and be on it's way, quite happily, "skipping" the any new input from the keyboard.

  7. #7
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Code:
      printf("First Name: ");
         scanf("%s", newEntry->firstName);
         printf("Last Name: ");
         scanf("%s", newEntry->lastName);
         printf("Grade: ");
         newEntry->grade = getchar();
         printf("Course Code: ");
         scanf("%d", newEntry->code);
    This is still erroring out on me. Is &newEntry->grade what I should be using? I figured this would give me the memory location of newEntry and not grade. Also even with getchar() it still skips input!

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    getchar does not skips input - it reads new line symbol left in the input stream by the previous scanf

    Code:
    scanf("%d", newEntry->code);
    you should pass a pointer to int, so the scanf could be able to update it
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    * noops's Avatar
    Join Date
    Jun 2008
    Posts
    108
    Yea, both grade and code are not pointers in your struct so you need to pass the address to scanf with &newEntry->grade.

    the -> resolves before & does so &newEntry->grade gives you the address of grade. If I recall correctly.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by noops View Post
    the -> resolves before & does so &newEntry->grade gives you the address of grade. If I recall correctly.
    That is correct.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Quote Originally Posted by Adak View Post
    That's the problem with scanf(), it will quit or "skip" unexpectedly. fgets() is the preferred method of getting input, but back to scanf():

    You need to add a getchar() (I like doing it immediately after the scanf(), but anyplace before the next scanf() will do), to get rid of the newline char which is still in the input buffer. Otherwise, the next call to scanf() will see that newline and say "Thanks a lot!", and be on it's way, quite happily, "skipping" the any new input from the keyboard.

    I finally understood what you said and added just a blank getchar() to soak up the '\n' from scanf...works perfectly. It is kind of funky though that I need one getchar() to soak up a '\n' and then immediately after another getchar to actually get my grade ><. Ill try out fgets now...I thought that was only for files though. Is stdin still considered a file on windows?

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by valaris View Post
    I finally understood what you said and added just a blank getchar() to soak up the '\n' from scanf...works perfectly. It is kind of funky though that I need one getchar() to soak up a '\n' and then immediately after another getchar to actually get my grade ><. Ill try out fgets now...I thought that was only for files though. Is stdin still considered a file on windows?
    stdin is a FILE *, yes.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    stdin considered a file on any operating system, at least as far as C is concerned it will be a FILE *.

  14. #14
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Again thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  2. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 10:01 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM