473,395 Members | 1,466 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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.
Mar 8 '07 #1
4 13584
DeMan
1,806 1GB
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
3,652 Expert 2GB
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
horace1
1,510 Expert 1GB
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
9,065 Expert Mod 8TB
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

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

Similar topics

5
by: kazack | last post by:
I am a little confused with code I am looking at. My c++ book does not go into passing a structure to a function so I pulled out a c book which does. and I do not understand the prototype verses...
2
by: Morgan | last post by:
Thanks to all of you because I solved the problem related with my previous post. I simply made confusion with pointers to pointers and then succeeded passing the reference to the first element...
5
by: Michael | last post by:
Hi, once I read here that it is not 'a good idea' to pass variables that are not initialized to a function. I have void something ( double *vector ); ....
17
by: Charles Sullivan | last post by:
The library function 'qsort' is declared thus: void qsort(void *base, size_t nmemb, size_t size, int(*compar)(const void *, const void *)); If in my code I write: int cmp_fcn(...); int...
4
by: Justine | last post by:
Can anyone help? e.g. when you open a vb project property page, in "Configuration Properties" You will see "Command line arguments" in "Start Options". Does anyone knows how to find a way to do...
1
by: Shawn | last post by:
As if it won't be clear enough from my code, I'm pretty new to C programming. This code is being compiled with an ANSI-C compatible compiler for a microcontroller. That part, I believe, will be...
17
by: ern | last post by:
I want to pass arguments to the main( ) function of my C program. Currently, I'm using main(char * args) But when I try to print args, I get an unhandled exception error. Is there a better,...
2
by: william.w.oneill | last post by:
I have an application that takes a few command line parameters. As recommended by others in this group, I'm using a named mutex to ensure that only one instance of the application is running. My...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.