Thread: Linked list - why this bit of code?

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    43

    Linked list - why this bit of code?

    Below is the first bit of a linked list program that I am trying to learn about. Can someone please tell me why the 4 lines in bold are required? I am confused as they seem to be just the name of functions that occur later in the program, however when I write programs I have never declared a function in this way before. Why would it be correct to write those 4 lines, in addition to the actual functions later on?

    Thankyou,
    Chris


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    /* node type for linked list */
    struct node {
        int val;
        struct node *next;
    };
    
    /* will point to beginning of list */
    struct node *list = NULL;
    
    
    void print_list(void);
    void add_item(char flag);
    void delete_item(void);
    void delete_list(void);
    
    int main(void) {
        char choice;
        char line[50];
    
        printf("Linked list demonstration (integers)\n");
    
        while (choice != 'q')
        {
    	printf("(1) Print contents of list\n");
    	printf("(2) Add item to beginning of list\n");
    	printf("(3) Add item to end of list\n");
    	printf("(4) Delete item from list\n");
    	printf("(5) Delete entire list\n");
    	printf("(q) Quit\n");
    	printf("Select an option: ");
    
    	fgets(line, sizeof(line), stdin);
    	sscanf(line, "%c", &choice);
    
    	switch (choice)
    	{
    	    case '1' :
    		print_list();
    		break;
    	    case '2' :
    		add_item('b'); /* add to beginning */
    		break;
    	    case '3' :
    		add_item('e'); /* add to end */
    		break;
    	    case '4' :
    		delete_item();
    		break;
    	    case '5' :
    		delete_list();
    		break;
    	    case 'q' :
    		printf("Cleaning up...\n");
    		delete_list();  /* unnecessary, but good habit */
    		break;
    	    default :
    		printf("Error - invalid input\n");
    		break;
    	}
        }
    
        return 0;
    }
    
    void print_list(void)
    {
        if.............

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Those are just function declarations. The actual function implementations are later in the file. The purpose of those declaractions are so that the compiler knows they exist somewhere in the file, and therefore you can call those functions from anywhere in that file.

    If you have the code:
    Code:
    int main(void)
    {
       test();
       return 0;
    }
    
    void test(void)
    {
       return;
    }
    The above code will not compile. It will give an error about calling the unknown function test(). The way around this is to add a function prototype at the beginning of the file:
    Code:
    void test(void);
    
    int main(void)
    {
       test();
       return 0;
    }
    
    void test(void)
    {
       return;
    }
    Now the code compiles, and works as expected.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    The purpose of those lines (they are called function prototypes) is to inform the compiler that there are functions with those names that take those particular arguments and return those particular types. It's primarily for the compiler to be able to do its job of type checking any arguments you may pass into the function and type checking any return values. Consider this:

    Code:
    int main()
    {
        char c;
        double d;
        d = foo(c);
        return 0;
    }
    
    /* Function foo declared somewhere below here */
    As the compiler reaches the call to the foo function it needs to do some type checking on the arugments passed into the function and the value returned from the function. It needs to do this so it can make a determination at compile time if one particular type can be converted into another and issue an appropriate warning/error if not. It cannot do this unless it knows what types the function expects as arguments and what type the function returns. If it has not come across the definition of the function yet or seen a function prototype, it does not know any of this. You can let the compiler do its job in two ways...

    1) Either by moving the definition of the foo function above main so that when the compiler gets to the place where the function is called, it already knows all about the arguments and return value or...

    2) Provide a prototype before the actual function definition and before the function is called for the first time in the code.
    Last edited by hk_mp5kpdw; 10-04-2005 at 06:19 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

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. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  3. How can I traverse a huffman tree
    By carrja99 in forum C++ Programming
    Replies: 3
    Last Post: 04-28-2003, 05:46 PM
  4. Linked List
    By jpipitone in forum C Programming
    Replies: 4
    Last Post: 03-30-2003, 09:27 PM
  5. problem with structures and linked list
    By Gkitty in forum C Programming
    Replies: 6
    Last Post: 12-12-2002, 06:40 PM