GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 27-Dec-2006, 11:28
Kas245 Kas245 is offline
New Member
 
Join Date: Dec 2006
Posts: 13
Kas245 is on a distinguished road

Need to know what this means


int main(int argc, char *argv[])


and some times


int main(int argc, char *argv[1])
  #2  
Old 27-Dec-2006, 11:53
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,310
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Need to know what this means


Quote:
Originally Posted by Kas245
CPP / C++ / C Code:
int main(int argc, char *argv[])
The function main is defined with two arguments:
The first argument is an int.
The second argument is an array of pointers to char.
The function main() returns an integer value to whatever program invoked it. (Typically an operating system shell, such as bash or cmd32.exe, but programs can also be invoked from other programs --- for example: with system() function calls.)

Upon entrance to this program, the value of argc tells us how many arguments were on the command line.

For Windows and Linux systems there is always at least one command-line argument: the program name itself. Sometimes argv[0] contains the entire path to the executable; sometimes only the program name itself --- it depends on the compiler. For embedded systems, it may be that the argc-argv mechanism isn't even implemented, so argc may be zero, and argv[0] will be a "NULL" pointer.

Anyhow, back to reality:

argv[0] is treated as a C-style "string" that tells us how the program was invoked.

argv[1], argv[2], ... argv[argc-1] are treated as C-style "strings" that are the command-line arguments that were present when the program was invoked.

Quote:
Originally Posted by Kas245
and some times
CPP / C++ / C Code:
int main(int argc, char *argv[1])

That's not the usual way of writing it, but in fact in a function definition, stuff inside the [brackets] in a 1-dimension array declaration is ignored, so the effect is exactly the same as the first example

You might have seen
CPP / C++ / C Code:
int main(int argc, char **argv)

In this case, argv is declared as a "pointer to a pointer to char". The effects on the C compiler (and, therefore, on the code) are exactly (yes, exactly) the same as the first example. It's because of how pointers are related to array index values in C (and C++) language notation.

Anyhow, you can try the following test:

CPP / C++ / C Code:
#include <stdio.h>

int main(int argc, char **argv) /* or use char *argv[] if you prefer*/
{
    int i;
    for (i = 0; i < argc; i++) {
        printf("argv[%d]: <%s>\n", i, argv[i]);
    }
    return 0;
}

With GNU gcc, I called the file "test.c" and compiled it, then ran it a few times

Code:
$ ./test argv[0]: <./test> $ ./test 12.34 oh, no argv[0]: <./test> argv[1]: <12.34> argv[2]: <oh,> argv[3]: <no> $ ./test Hello 12.34 "It's kind of fun, huh?" argv[0]: <./test> argv[1]: <Hello> argv[2]: <12.34> argv[3]: <It's kind of fun, huh?>

Lines beginning with '$' are the command lines on my system and they show what I entered.
The other lines show the program's output.

Regards,

Dave

Footnote:
The program that I gave is a valid C program and is also a valid C++ program. If you want to do things the "C++" way, then you can use "cout << " instead of printf(). (Of course the header will change to <iostream>, and you may have to give some namespace information also.)
  #3  
Old 28-Dec-2006, 07:37
Kas245 Kas245 is offline
New Member
 
Join Date: Dec 2006
Posts: 13
Kas245 is on a distinguished road

Re: Need to know what this means


thanks. understanding

but i run this in C++ it just says the program file name and in C it comes up with an error " main function not defined" .
  #4  
Old 28-Dec-2006, 08:59
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,310
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Need to know what this means


Quote:
Originally Posted by Kas245
thanks. understanding

but i run this in C++ it just says the program file name and in C it comes up with an error " main function not defined" .

Did you paste the exact program from my post into your text editor? How are you compiling the program?

Just to be sure that what I posted can work, I pasted the code from my previous post to my text editor and saved it.

This file:
Quote:
Originally Posted by davekw7x
CPP / C++ / C Code:
#include <stdio.h>

int main(int argc, char **argv) /* or use char *argv[] if you prefer*/
{
    int i;
    for (i = 0; i < argc; i++) {
        printf("argv[%d]: <%s>\n", i, argv[i]);
    }
    return 0;
}
I named the file "test.c" and compiled it (as a C program) with Microsoft Visual C++ version 6, Microsoft Visual C++ 2005 Express, Borland bcc32, GNU gcc.

I copied the file to "test.cpp" and compiled it (as a C++ program) with Microsoft Visual C++ version 6, Microsoft Visual C++ 2005 Express, Borland Bcc32, GNU g++

All were successful in replicating my previous results.

Here are a few runs on my Windows XP platform after compiling with the Borland compiler:

Code:
F:\test>test argv[0]: <F:\test\test.exe> F:\test>test 12.34 oh, no argv[0]: <F:\test\test.exe> argv[1]: <12.34> argv[2]: <oh,> argv[3]: <no> F:\test>test Hello 12.34 "It's kind of fun, huh? argv[0]: <F:\test\test.exe> argv[1]: <Hello> argv[2]: <12.34> argv[3]: <It's kind of fun, huh?>

Here are the results from Microsoft Visual C++ 2005 Express:
Code:
F:\test>test argv[0]: <test> F:\test>test 12.34 oh, no argv[0]: <test> argv[1]: <12.34> argv[2]: <oh,> argv[3]: <no> F:\test>test Hello 12.34 "It's kind of fun, huh" argv[0]: <test> argv[1]: <Hello> argv[2]: <12.34> argv[3]: <It's kind of fun, huh>

Note the differences in how argv[0] is presented. As I mentioned, that is system-dependent. The other arguments are handled identically, as they should be according to the C (and C++) standard.

Regards,

Dave
  #5  
Old 28-Dec-2006, 12:10
Kas245 Kas245 is offline
New Member
 
Join Date: Dec 2006
Posts: 13
Kas245 is on a distinguished road

Re: Need to know what this means


For the C i used icc win32 and
C++ i used vs2005
  #6  
Old 29-Dec-2006, 08:59
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 5,310
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Need to know what this means


Quote:
Originally Posted by Kas245
For the C i used icc win32 and
C++ i used vs2005

How did you compile the code? How did you execute it? I executed from a command line. (Open a console window and navigate to the directory where the executable is located and enter the program name with command-line arguments as I showed.)

If the code that I posted gave a compile error, then your compiler setup is incorrect. I have no knowledge of icc win32, but all C compilers should compile it, assuming you have a valid installation.

Have you compiled any other programs with your setup (Hello, world, or whatever)?

Regards,

Dave
  #7  
Old 31-Dec-2006, 02:49
Kas245 Kas245 is offline
New Member
 
Join Date: Dec 2006
Posts: 13
Kas245 is on a distinguished road

Re: Need to know what this means


well i am sure it is the compilar. thanks
  #8  
Old 31-Dec-2006, 10:24
davis
 
Posts: n/a

Re: Need to know what this means


Quote:
Originally Posted by davekw7x
CPP / C++ / C Code:
#include <stdio.h>

int main(int argc, char **argv) /* or use char *argv[] if you prefer*/

Since this is a C++ forum, I usually prefer to see Stroustrup's own...

CPP / C++ / C Code:
int main(int argc, char* argv[])

...note cuddling the pointer "*" with the typename.


:davis:
 
 

Recent GIDBlogInstall Adobe Flash - Without Administrator Rights by LocalTech

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
What does =0 in Pure virtual function means? Poolan C++ Forum 4 26-May-2006 14:58
Can you please tell me what this error means? febrarierose C++ Forum 1 23-May-2006 23:16
Need help getting started on a database search program Heresy C Programming Language 13 03-May-2006 13:56
What does this warning means? renato_anything C Programming Language 1 29-Apr-2005 20:55

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 15:54.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.