Results 1 to 11 of 11

Thread: int main()

  1. #1

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    Question

    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
    Amon Ra
    The Power of Learning.

  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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.
    Harry.

    "From one thing, know ten thousand things."

  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  4. #4

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    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
    Amon Ra
    The Power of Learning.

  5. #5
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    You don't need them unless you want to take command line parameters.
    Harry.

    "From one thing, know ten thousand things."

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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;
    }
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    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?
    Amon Ra
    The Power of Learning.

  8. #8
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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.
    Harry.

    "From one thing, know ten thousand things."

  9. #9

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    Red face 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
    Amon Ra
    The Power of Learning.

  10. #10

    Thread Starter
    Hyperactive Member Amon Ra's Avatar
    Join Date
    Feb 2001
    Location
    In some cave on Uranus...
    Posts
    500

    Unhappy ??

    Please, again, when should i judje myself as able to do windows programming??
    Dont just read this, please tell me Thanx
    Amon Ra
    The Power of Learning.

  11. #11
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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.
    Harry.

    "From one thing, know ten thousand things."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width