Thread: arguments

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    67

    arguments

    hey,
    I'm re-writing a program i made,
    now i want to put a function into a header file, zo far so good, bt how do i work with arguments in this way:
    Code:
    *** main.c ***
    #include "lib.h"
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
        // roep de check_for_error functie aan uit lib.h
        check_for_error);
        
        // roep de check_hosts functie aan uit lib.h
        check_hosts();
        
        // roep de functie check_files aan uit lib.h
        check_files();
        
        return 0;
    }
    and the header file
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    
    int check_for_error(int argc, char *argv[])
    {
        // kijk of er een boodschap meegestuurd word
        if (argv[1] == NULL)
        {
            printf("Geef een boodschap om over te sturen.\n");
        }
        
        
        
        printf("ERROR CHECKING ROUTINE\n");
        return 0;
    }
    
    int check_hosts()
    {
        printf("HOST CHECKING ROUTINE\n");
        return 0;
    }
    well as you can see i want to check parameters given to the program but i get this error:
    gcc.exe -c main.c -o main.o -I"C:/Program Files/Dev-Cpp/include"

    main.c: In function `main':
    main.c:8: too few arguments to function `check_for_error'

    make.exe: *** [main.o] Error 1

    Execution terminated
    anyone some ideas for me?
    thanx in advance

  2. #2
    Registered User
    Join Date
    Mar 2003
    Posts
    143
    You need to pass the arguments into the function so your main() function should be:
    Code:
    /*** main.c ***/
    #include "lib.h"
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
        // roep de check_for_error functie aan uit lib.h
        check_for_error(argc, argv);
        
        // roep de check_hosts functie aan uit lib.h
        check_hosts();
        
        // roep de functie check_files aan uit lib.h
        check_files();
        
        return 0;
    }
    Also it is not usual to put function definitions in a header file (it will work though). The usual way if you want the functions to be part of different module would be to declare the functions in lib.h:

    Code:
    /*** lib.h ***/
    #ifndef LIB_H
    #define LIB_H
    
    int check_for_error(int argc, char *argv[]);
    int check_hosts();
    
    #endif /* LIB_H */
    And link a seperate file, lib.c into your project which contains the function definitions for check_for_error() and check_hosts().

    Personally I don't think that these functions warrant their own module and would put them in main.c - but that's just a matter of personal taste I guess...
    DavT
    -----------------------------------------------

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    67

    thanx

    well you are right, but I like to learn how to work with header files and such

    thanx for ure help

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    67

    yey

    Well i've done what you recommanded (using another file (lib.c) in your example and it all works like a charm, the pieces of code a showed arent the entire functions, they are really long and are helpful when i want to write other programs,
    but thanx loads!

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Also it is not usual to put function definitions in a header file (it will work though).
    Only as long as you include the header in only one module.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 03:27 PM
  3. command line arguments
    By vurentjie in forum C Programming
    Replies: 3
    Last Post: 06-22-2008, 06:46 AM
  4. Replies: 10
    Last Post: 09-27-2005, 12:49 PM
  5. NULL arguments in a shell program
    By gregulator in forum C Programming
    Replies: 4
    Last Post: 04-15-2004, 10:48 AM