Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining own main functions arguments argc and argv

Tags:

c++

qt

People also ask

What is argc and argv in main () functions?

The first parameter, argc (argument count) is an integer that indicates how many arguments were entered on the command line when the program was started. The second parameter, argv (argument vector), is an array of pointers to arrays of character objects.

What does int argc char * argv [] mean in C?

Command-line Arguments: main( int argc, char *argv[] ) Here argc means argument count and argument vector. The first argument is the number of parameters passed plus one to include the name of the program that was executed to get those process running.

How is argv defined?

What is ARGV? As a concept, ARGV is a convention in programming that goes back (at least) to the C language. It refers to the “argument vector,” which is basically a variable that contains the arguments passed to a program through the command line.


If you want to be insanely pendantic, then you want something like the following. The key points are that argv is not const, argv is NULL terminated, argc is the number of usable elements in argv including the program name. It is required to be modifiable so you cannot use string literals - argv[i] is required to point to a modifiable array of characters.

int my_main() {
    char  arg0[] = "programName";
    char  arg1[] = "arg";
    char  arg2[] = "another arg";
    char* argv[] = { &arg0[0], &arg1[0], &arg2[0], NULL };
    int   argc   = (int)(sizeof(argv) / sizeof(argv[0])) - 1;

    QApplication the_application(argc, &argv[0]);
    return the_application.run();
}

The Standard (ISO/IEC 9899:1999 section 5.1.2.2.1) states that the following is true about argc and argv in a hosted environment:

  • The value of argc shall be nonnegative.
  • argv[argc] shall be a null pointer.
  • If the value of argc is greater than zero, the array members argv[0] through argv[argc-1] inclusive shall contain pointers to strings, which are given implementation-defined values by the host environment prior to program startup from elsewhere in the hosted environment. If the host environment is not capable of supplying strings with letters in both uppercase and lowercase, the implementation shall ensure that the strings are received in lowercase.
  • If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name; argv[0][0] shall be the null character if the program name is not available from the host environment. If the value of argc is greater than one, the strings pointed to by argv[0] through argv[argc-1] represent program parameters.
  • The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination.

QApplication states the following:

Warning: The data referred to by argc and argv must stay valid for the entire lifetime of the QApplication object. In addition, argc must be greater than zero and argv must contain at least one valid character string.

Note: argc and argv might be changed as Qt removes command line arguments that it recognizes.


Quick and dirty, but working for QApplication:

char *argv[] = {"program name", "arg1", "arg2", NULL};
int argc = sizeof(argv) / sizeof(char*) - 1;

For a more complete and C standard conforming solution see D.Shawley's answer.

Why your solution doesn't work is simple:

array[i][j] results in a i*j matrix. But what you actually want is an array with pointers to strings in it.


Why are you concerning your self with the size of the text in argv, I would just let the compiler do it:

int argc = 1;
char* argv[] = {"Hello Qt!"}; // exactly as it is defined in main

How about...

int argc = 2;
const char* argv[] ={"program","first-argument"}; 

...or if you need them to be non-const...

int argc = 2;
char* argv[] ={strdup("program"),strdup("first-argument")};