Thread: Please tell me what this function is!

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    54

    Please tell me what this function is!

    Hello,

    I have this function and don't understand it.

    void functionname (int argc, char **argv)
    {

    //doing somthing with argc and argv

    }
    In the main program, I want to call this functioname but I don't know hoaw to pass the value for argc and argv. If anyone knows this, please explain for me. Thank you very much!

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Your main function should look something like this:
    Code:
    int main( int argc, char** argv )
    {
        // Call the "functionname" function here, pass it the same values
        // that were passed into "main".
    
        functionname( argc, argv );
    
        return 0;
    
    }
    "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

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    54
    Thank you! Would you please tell me a little bit more about argc and char** argv ?

  4. #4
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    These two values hold information about the command-line arguments that are passed into the program. The argc integer value stores the number of command-line parameters that have been passed into the program. This value is always at least 1 since the name of the program itself is counted as the first comman-line argument. The argv parameter is a pointer to an array of argc character pointers, each of which in turn points to an individual command-line argument. Try this little program to see a little about how this works:

    Code:
    #include <stdio.h>
    
    int main( int argc, char** argv )
    {
        int iLoop;
    
        for( iLoop = 0; iLoop < argc; iLoop++ )
            printf( "Argument #%d is %s.\n", iLoop+1, argv[iLoop] );
    
        return 0;
    }
    If you build this program, open up a console window, and go to the directory where the program is located, you can then try running the program with a various number of command-line parameters. Lets say the program is called "MyProgram". If you were to type in "MyProgram Hello 123" then the program should output the following:

    Argument #1 is MyProgram.
    Argument #2 is Hello.
    Argument #3 is 123.

    If you run the program as "MyProgram "Hello 123"" then the program should output:

    Argument #1 is MyProgram.
    Argument #2 is Hello 123.
    "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

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    54
    Thank you very much! I got it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM