Like Tree1Likes
  • 1 Post By ZuK

Multi-File Linked List

This is a discussion on Multi-File Linked List within the C Programming forums, part of the General Programming Boards category; The point of this assignment was to get used to working with multiple files in C (part of a linked-list). ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    11

    Post Multi-File Linked List

    The point of this assignment was to get used to working with multiple files in C (part of a linked-list). The files are separate and only work when compiled together. Here they are:
    Code:
    #include "prog10.h"
    //main.c
    int main (void)
    {
        PERSON *head = NULL;
    
        head = createList();
        print_list(head);
        release_memory(head);
        printf("Done\n");
        return 0;
    }
    Code:
     
    PERSON* createList(void)
    {//createList.c
            char answer;
            PERSON *current, *pervious, *head=NULL;
            while(1)
            {
            printf("Add a person to the List?[y/n]\n");
            scanf("%c", &answer);
            if(answer=='n')
                    return head;
            current =  (PERSON *)malloc(sizeof(PERSON) );
            printf("Enter a name:");
            scanf("%s", current->name);
            printf("Enter persons age:");
            scanf("%i", &current->age);
            while(getchar() != '\n');
            if(head==NULL)
                    head=current;
            else
                    previous->next_ptr=current;
            current->next_ptr=NULL;
            previous=current;
    }
    }
    Code:
    void print_list(PERSON *person_ptr)
    {
    //p10.c
    while(person_ptr != NULL)
    {
    printf("Name: %s", person_ptr->name);
    printf("Age: %i", &person_ptr->age);
    person_ptr=person_ptr->next_ptr;
    }
    }
    Code:
    #include "prog10.h"
    void release_memory(PERSON *person_ptr)
    {//releaseMemory.c
        PERSON *tmp_next_ptr;
        while (person_ptr != NULL)
        {
            tmp_next_ptr = person_ptr->next_ptr;
            free(person_ptr);
            person_ptr = tmp_next_ptr;
        }
        puts("Memory Released");
    }
    Every time I compile the program, I get an error in the first line of p10.c. It says that there is an error before ' * ' in function print_list. The next error says that NULL has not been declared. Can anyone help?

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    What are the exact errors? (copy'n'paste them)
    How are you compiling the program?

    Aren't you missing some #include directives? Where's "prog10.h"?

    Bye, Andreas

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    11
    I'm using vi; it says specifically: expected ' ) ' before ' * ' token. Here is prog10.h:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct person {
        char name[20];
        int age;
        struct person *next_ptr;
    } PERSON;
    
    PERSON* createList(void);
    void    print_list(PERSON *head);
    void    release_memory(PERSON *);

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by andrey.117 View Post
    I'm using vi; it says specifically: expected ' ) ' before ' * ' token.
    vi is a text editor not a compiler.
    Is this the complete error message (i.e. no line number and filename mentioned)?

    And you still haven't answered the following questions:
    How are you compiling the program? (What commands do you use?)
    Aren't you missing some #include directives?

    Bye, Andreas

  5. #5
    Registered User
    Join Date
    Mar 2013
    Posts
    11
    To compile, I type in: gcc main.c createList.c p10.c releaseMemory.c -o p10

    The #include directives are in prog10.h

    The error points to the first line of p10.c, which is:
    void print_list(PERSON *person_ptr)

  6. #6
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by andrey.117 View Post
    The #include directives are in prog10.h
    That's your problem. As shown, the files "p10.c" and "createList.c" don't know anything about the declarations in "prog10.h". You need to include the header in every source file which uses these declarations.

    Bye, Andreas

  7. #7
    Registered User
    Join Date
    Mar 2013
    Posts
    11
    When I add prog10.h to every file, it just gives me more errors, especially in createList.c. It tells me that I haven't declared PERSON, current, previous, or head, when I obviously did.

  8. #8
    Registered User
    Join Date
    Mar 2013
    Posts
    11
    Sorry about that, I just solved it, part of the problem was me not adding the prog10.h to every file; thank you for your reply.

  9. #9
    ZuK
    ZuK is online now
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by andrey.117 View Post
    When I add prog10.h to every file, it just gives me more errors, especially in createList.c. It tells me that I haven't declared PERSON, current, previous, or head, when I obviously did.
    I woul expect multiple defines.
    You need include guards in the header.
    Kurt
    AndiPersti likes this.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked list from file
    By kapil1089thekin in forum C++ Programming
    Replies: 2
    Last Post: 07-27-2010, 12:23 PM
  2. Linked List to file
    By RoseGirl in forum C Programming
    Replies: 3
    Last Post: 04-24-2009, 03:05 PM
  3. Replies: 6
    Last Post: 03-02-2005, 01:45 AM
  4. file into a linked list
    By pilecom in forum C Programming
    Replies: 2
    Last Post: 10-17-2002, 12:14 AM
  5. file i/o -linked list
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 12-14-2001, 10:13 PM

Tags for this Thread


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