passing arguments to main

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • phoolpreet
    New Member
    • Mar 2007
    • 9

    passing arguments to main

    hi guys
    i m stuck with the problem of passing arguments to main.
    we can define argc and argv and put statements in main that use these arguments.
    but how to set these value while running a programme.
    how to pass value to main.
    i m using linux g plus plus compiler.
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    main takes as arguments an array and it's size, where the array is the command line arguments you pass.

    So when you open a program by saying:

    myProgram -p file.txt

    "-p" snf "file.txt" are stored in the array you declare main to take (in this case argv)....

    so you would be passed 3 (the size of the array) and the array (argv) would contain:

    argv[0]= your class
    argv[1] = "-p"
    argv[2] = "file.txt"

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      would myProgram also be included? It was my impression that, since myProgram was the .exe, using myProgram would instruct the program to run, and everything after it would be the arguments.

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        Originally posted by Ganon11
        would myProgram also be included? It was my impression that, since myProgram was the .exe, using myProgram would instruct the program to run, and everything after it would be the arguments.
        If program MyProg.exe is started at the Command Prompt

        c:> MyProg file_1 file_2 -lower

        the parameters to main()
        Code:
        int main(int argc, char *argv[])
        are :
        argc an int which contains the number of command line parameters;
        *argv[] is an array of pointers to char (character strings which contain the parameters).

        In the command line "MyProg file_1 file_2 -lower" the value of argc would be 4 and argv would appear as follows:
        Code:
               element        contents              contents
                  n          of argv[n]            of *argv[n]
                           +--------------+         +--------+    
               argv[0]     ¦ pointer to _ +---------¦"MyProg"¦
                           ¦              ¦         +--------+ 
                           +--------------¦          
                           ¦              ¦         +--------+
               argv[1]     ¦ pointer to _ +---------¦"file_1"¦
                           ¦              ¦         +--------+ 
                           +--------------¦          
                           ¦              ¦         +--------+
               argv[2]     ¦ pointer to _ +---------¦"file_2"¦
                           ¦              ¦         +--------+ 
                           +--------------¦          
                           ¦              ¦         +--------+
               argv[3]     ¦ pointer to _ +---------¦"-lower"¦
                           ¦              ¦         +--------+ 
                           +--------------¦          
                           ¦              ¦                  
               argv[4]     ¦    NULL      ¦               
                           +--------------+
        Therefore the elements of argv are pointers to strings which contain the command line parameters (null terminated). By convention argv[0] points to a string which contains the name by which the program was invoked and element argv[argc] is the NULL pointer.

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Originally posted by horace1
          By convention argv[0] points to a string which contains the name by which the program was invoked and element argv[argc] is the NULL pointer.
          Not by convention, by specification in the C/C++ standards. Any compiler that does not do this is non-conforming.

          Comment

          Working...