QwertyCoolGuy
QwertyCoolGuy

Reputation: 15

C command line arguments - Where is the argv char array?

I wish to refer to the following code related to my question.

#include <stdio.h>  
int main(int argc, char *argv[]) {           
    printf("%c\n", argv[1][1]);                
    return 0;
}

usually prior to creating a pointer, there must exist the variable in the first place. But in C command line arguments, *argv[] is not refered to an already defined char argv but it is actually a two dimensional array that contains not only addresses but also elements of command line arguments. How can this happen. I would appreciate the theory behind this.

My second question is how could a pointer array come to contain the elements of the command line arguments?

Upvotes: 0

Views: 800

Answers (6)

VolAnd
VolAnd

Reputation: 6407

Function parameters are variable declarations (e.g. int argc is a local variable in main function). And argv[] is array of pointers that stores the addresses of strings. Memory for pointers and strings allocated by code that prepared by compiler to call your main function, so only one int and one pointer are given to local variables of main function (argv and argc).

Upvotes: 0

Leonard Michlmayr
Leonard Michlmayr

Reputation: 365

When the system (OS and standard library) starts your program by calling the function main (which you have to provide) it sets up an array containing pointers to the command-line argument strings. main will be called with two arguments: the number of command-line arguments and the array itself.

Reference: https://www.gnu.org/software/libc/manual/html_node/Program-Arguments.html

Upvotes: 0

Mark Shevchenko
Mark Shevchenko

Reputation: 8207

There is some code that runs before the main. Actually this code calls main function.

What does this code? F.e. it initiates such variables as stdin, stdout, stderr. It parses a command line and prepares argv array.

See http://en.wikipedia.org/wiki/Crt0 for more info.

Upvotes: 1

paulsm4
paulsm4

Reputation: 121809

The short answer is:

1) The operating system passes the new process the argc, argv[] ... and other startup parameters

2) Most C implementations have a "CRT0" library call that gets executed before "main()" gets executed. One of CRT0's jobs is to pass argv[] from, the OS program loader to the C program's "main()".

Here are more details:

http://wiki.osdev.org/Creating_a_C_Library

The first and most important thing to implement in a C library is the _start function, to which control is passed from your program loader. It's task is to initialize and run the process. Normally this is done by initializing the C library (if needed), then calling the global constructors, and finally calling exit(main(argc, argv)).

...

the_start function calls initialize_standard_library. Note how the program loader register usage nicely fits the x86_64 SysV calling convention, and how the initialize_standard_library(int argc, char* argv[], int envc, char* envp[]) function accepts the same arguments as _start.

See also man execvp(3).

Upvotes: 0

Andras
Andras

Reputation: 3055

The C runtime library that calls the main() function (which is just a function like any other) also sets up and passes in 3 parameters: (argc, argv, envp) This is built into the bootstrap code that starts running all compiled C programs; by the time main() is called, the command line has been read and tokenized and pointed to by argv.

Argv is not two-dimensional, is a one dimensional array of char* pointers to text; each text string has been copied into its own array of char stored elsewhere.

Upvotes: 0

Rob
Rob

Reputation: 1974

There is some implementation-dependent manner in which an executable is started and by which it either receives or retrieves information from the host environment. For a C program, part of that process results in command line arguments being stored in an array. Then main() is called in some way, and appropriate values of argc and argv are given.

That can be done by the host system itself (e.g. the operating system sets things up, then calls the program's main() directly) or it can be done after the programs executable commences executing (e.g. there is startup code in the program executable itself that retrieves command line information, packages it up, and then calls main()).

Upvotes: 0

Related Questions