By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 791 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

passing arguments to main

P: 9
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.
Mar 8 '07 #1
Share this Question
Share on Google+
4 Replies


DeMan
100+
P: 1,806
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"
Mar 8 '07 #2

Ganon11
Expert 2.5K+
P: 3,652
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.
Mar 8 '07 #3

Expert 100+
P: 1,510
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()
Expand|Select|Wrap|Line Numbers
  1. int main(int argc, char *argv[])
  2.  
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:
Expand|Select|Wrap|Line Numbers
  1.        element        contents              contents
  2.           n          of argv[n]            of *argv[n]
  3.                    +--------------+         +--------+    
  4.        argv[0]      pointer to _ +---------"MyProg"
  5.                                           +--------+ 
  6.                    +--------------          
  7.                                           +--------+
  8.        argv[1]      pointer to _ +---------"file_1"
  9.                                           +--------+ 
  10.                    +--------------          
  11.                                           +--------+
  12.        argv[2]      pointer to _ +---------"file_2"
  13.                                           +--------+ 
  14.                    +--------------          
  15.                                           +--------+
  16.        argv[3]      pointer to _ +---------"-lower"
  17.                                           +--------+ 
  18.                    +--------------          
  19.                                                    
  20.        argv[4]         NULL                     
  21.                    +--------------+                                   
  22.  
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.
Mar 8 '07 #4

Banfa
Expert Mod 5K+
P: 8,916
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.
Mar 8 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.