473,545 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

passing arguments to main

9 New Member
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 13595
DeMan
1,806 Top Contributor
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 Recognized Expert Specialist
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 Recognized Expert Top Contributor
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 Recognized Expert Moderator Expert
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
34346
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 the actual function call. I will post the code below of the structure, the prototype and and function call and if someone can explain this I would...
2
4296
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 pointer. :-)) You were right about by mixed code: quickly writing an example to clarify the matter I made a C++ program, sorry for the inconvenience....
5
3902
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
3576
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 (*fcmp)() = &cmp_fcn; qsort(..., fcmp); then everything works. But if instead I code qsort as:
4
1954
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 arguments passing. Thank you. ----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==---- http://www.newsfeed.com The #1...
1
3261
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 irrelavent. My syntax is surely where I am going wrong. I'd like to be able to call this routine to read different values from another device. ...
17
3242
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, safer way to pass args to main (from another module) ?
2
4233
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 question is how to elegantly pass a command line parameter from Instance_B to Instance_A where Instance_A was running prior to Instance_B. For...
0
7410
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7668
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7437
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7773
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3466
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1901
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1025
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
722
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.