![]() |
|
#1
|
|||
|
|||
Need to know what this meansint main(int argc, char *argv[])
and some times int main(int argc, char *argv[1]) |
|||
|
#2
|
|||
|
|||
Re: Need to know what this meansQuote:
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:
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:
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:
With GNU gcc, I called the file "test.c" and compiled it, then ran it a few times Code:
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
|
|||
|
|||
Re: Need to know what this meansthanks. 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
|
|||
|
|||
Re: Need to know what this meansQuote:
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:
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:
Here are the results from Microsoft Visual C++ 2005 Express: Code:
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
|
|||
|
|||
Re: Need to know what this meansFor the C i used icc win32 and
C++ i used vs2005 |
|
#6
|
|||
|
|||
Re: Need to know what this meansQuote:
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
|
|||
|
|||
Re: Need to know what this meanswell i am sure it is the compilar. thanks
|
|
#8
|
|||
|
|||
Re: Need to know what this meansQuote:
Since this is a C++ forum, I usually prefer to see Stroustrup's own... CPP / C++ / C Code:
...note cuddling the pointer "*" with the typename. :davis: |
Recent GIDBlog
Install Adobe Flash - Without Administrator Rights by LocalTech
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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