-
What is that for?
int main(int argc, char* argv[])
in the tuts it says:
The interger, argc is the ARGument Count (hence argc). It is the number of arguments passed into the program from the command line, including the path to and name of the program.
The array of character pointers is the listing of all the arguments. argv[0] is entire path to the program including its name. After that, every element number less than argc are command line arguments. You can use each argv element just like a string, or use argv as a two dimensional array.
I dont understand any of this! Please help me:):):) Thanx
-
Well basically, say at the command line you type
Code:
C:\DirName\FileName.exe -argthing -something
In this case you have supplied 2 arguments to your program (your program is FileName.exe, and it's located in C:\DirName\)
So from your description (I haven't used command line parameters before), argc will be 3, argv[0] will be the char* string "C:\DirName\FileName.exe", argv[1] will be "-argthing" and argv[2] will be "-something". What you do with these parameters is really up to you. Generally you would check if they were recognised keywords, and if they aren't either ignore them (if your program can do some default action) or print out a description of acceptable arguments.
So essentially, argc is the number of arguments supplied +1, argv[0] is the file path and file name that the user typed, and argv[1] to argv[argc-1] are the arguments you want.
-
Actually, argv[0] is the full path, not just what was typed.
Under windows, VC++ also supports:
Code:
int main(int argc, char **argv, char **envv)
NB: I used **argv out of personal preference, but *argv[] is still perfectly legal. envv stores an array of environment variables, so you don't need to use getenv()...not totally sure about the usefulness of envv, actually :rolleyes:
-
So...
So, sorry, but it still not really clear. BTW, do i really need them? if later i want to go into windows programming?
Thanx
-
You don't need them unless you want to take command line parameters.
-
Hate to be the one to break this to you, but in Windows programming main is not used, and is replaced by an even more painful creature:
Code:
int WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow);
You don't have to have argc, argv, and envv. In fact I don't use envv at all, and I don't know of any code that does. If you need to process your program's arguments, then you need argc and argv.
Code:
#include <iostream.h>
int main(int argc, char **argv) {
int i;
for(i = 0; i < argc; i++) {
cout << i << ":" << argv[i] << endl;
}
return 0;
}
-
Hmmm...
LOL Thanks for the positive intro, parksie :)
Anyways, so i will just leave that aside. The main thing i am supposed to know to make win program is classes, right?
-
Well you can make Windows programs with just straight C (I think) so classes are useful but not essential. You will need to know all about pointers though.
-
Pointers
Right now, the only thing i can do with pointers is get the value in a specific address, or get the actual address of a variable in the memory. Is there more than that?(i am sure there is :)
-
??
Please, again, when should i judje myself as able to do windows programming??
Dont just read this, please tell me :) Thanx
-
Umm, well I can't really tell you, since I don't do much Windows programming, mainly plain C++ and DirectX. Do you understand the relationships between pointers, arrays and strings? That's probably the hardest bit to get to grips with, although it's one of the basics.
If you think you know more or less all about console programming, then you will know enough to start Windows programming. It's just a matter of understanding the language pretty well before you dive into Windows apps and get confused, because there are a lot of aspects to Windows programming that are not really anything to do with C++, they're to do with the O/S.