0

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?

3
  • 3
    This is done by the operating system and the C runtime library's startup code. Commented Feb 8, 2015 at 7:13
  • 2
    Also, consider that a function has to be called in order to execute – but who calls your main()? Magic! As to "how could a pointer array come to contain the elements of the command line arguments" – since the command line arguments are strings, and in C, strings are usually referred to by a pointer to their first character. Commented Feb 8, 2015 at 7:18
  • Related if not a duplicate: stackoverflow.com/q/12332/694576 Commented Feb 8, 2015 at 9:27

6 Answers 6

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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()).

Comments

0

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.

Comments

0

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).

Comments

0

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

Comments

0

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.