Thread: Passing arguments to main?

  1. #1
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589

    Question Passing arguments to main?

    I was reading details on Queues and Stacks and I have created many questions. How and why do people pass arguments to main. Where do they come from? Do multiple mains have the capability of talking to each other? Below is the code for a Queue that created the interest in this. I found it from the link also located below.
    Link
    Code:
     
    int main(int argc, char *argv[])
    {
        Queue dataq;
        char cmd;
        QElement *data;
        int pos;
    
        do {
           cmd = getcommand();
           switch (cmd) {
              case 'E': data = new QElement(getvalue());
                        dataq.Enqueue(data);
                        cout << "Enqueued " << data->Getvalue() << endl;
                        break;
              case 'D': data = dataq.Dequeue();
                        if (data == NULL) {
                           cout << "Unable to dequeue from empty queue" << endl;
                        } else {
                           cout << "Dequeued " << data->Getvalue() << endl;
                        }
                        break;
              case 'P': pos = getvalue();
                        if (pos > dataq.Getsize()) {
                           cout << "There are only " << dataq.Getsize();
                           cout << " elements currently in the queue" << endl;
                        } else {
                           data = dataq.PeekQ(pos);
                           cout << "Value " << data->Getvalue();
                           cout << " is at position " << pos << endl;
                        }
                        break;
              case 'Q': break;
              default:  cout << "Invalid command: " << cmd << endl;
                        break;
           }
        } while (cmd != 'Q');
    
        cout << "Bye!" << endl;
        return 0;
    }
    Last edited by xviddivxoggmp3; 10-15-2003 at 11:38 PM.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  2. #2
    Registered User Azuth's Avatar
    Join Date
    Feb 2002
    Posts
    236
    The operating system passes arguments to main from the command line when you run the program, a lot of development environments will let you set these command line arguments for testing purposes.

    i.e.

    C:\>myprog.exe test data

    ...will pass test & data as strings to the argv array and count them with argc. The first item in the argv array is the program that was executed. The variables can be called anything (so long as you don't change the type), but argc & argv are standard.
    Demonographic rhinology is not the only possible outcome, but why take the chance

  3. #3
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    what type of messages would the os pass to main?
    do you have any examples that could help support what you are saying. I'm new at this and need something to help me visualize what helpful details the os would send. maybe like system configurations for manipulations of system hardware, but for a queue to receive system details it kind of blows my mind at what would be needed from the os.
    C:\>myprog.exe test data
    does this mean if i use passing like this program does can I test all my programs like this? I do not see where in the code that the program even converts these details. it looks like to me that it acquires data from the os, but doesn't use it. isn't that a waist?
    Last edited by xviddivxoggmp3; 10-16-2003 at 03:21 AM.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you make main non-void, then any command line parameters you specify when you run it are available to you. If you don't, they're not. It's that simple. You don't have to allocate space or anything for them, they are just there for you. Either use them or ignore them, it doesn't make any difference.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Amateur
    Join Date
    Sep 2003
    Posts
    228
    What I'm going to say is true under Windows at least.
    The OS does not pass anything to main(), main() is an user-defined C function and as it, can take any parameters you want it to take, however, the compiler can but don't have to warn you if you write something silly.
    Windows will only call the entry-point of the program with no arguments. But in the C implementation, the compiler does the task of calling main with the right things.
    If you ever use MSVC, you'll notice that you can define the entry-point of your executable but there are limitations.
    Last edited by lyx; 10-16-2003 at 04:01 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory leaks problem in C -- Help please
    By Amely in forum C Programming
    Replies: 14
    Last Post: 05-21-2008, 11:16 AM
  2. passing params to main()
    By mike11 in forum C++ Programming
    Replies: 14
    Last Post: 06-21-2005, 12:36 PM
  3. Passing arguments to script.....
    By suwie in forum C Programming
    Replies: 5
    Last Post: 09-25-2004, 11:10 PM
  4. help Passing Arguments
    By cis in forum C Programming
    Replies: 3
    Last Post: 03-08-2002, 08:33 PM
  5. passing a file name to main()
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 09-06-2001, 04:08 PM