Understanding of main (INT argc, char * argv [])

Source: Internet
Author: User

In the past, I had some understanding about the parameter settings in the main function. I thought it was very simple, not that argc is the number of parameters, but argv is a string array. However, the problem of writing multiple processes yesterday was indeed troublesome, today, I think about a lot of problems. The problems involved are also in all aspects. Next we will make a careful analysis.

Problem description: When a main process starts multiple sub-processes, it needs to transmit parameters to the sub-process program. We know that if a parameter is passed to the program, that is, a parameter is passed to the main function, you need to use the argv array to store parameters. when reading this parameter, you need to access the argv array. For this problem, I have never been able to debug it. Today I think it is because I have accessed the wild pointer.

Next, let's take a look at the code. This Code does not have any operations and directly prints the argv information of the array.

#include <stdio.h>#include <windows.h>int main(int argc,char* argv[]){for (int i=0;i<argc;i++){printf("%s\n",argv[i]);}system("pause");return 0;}

The running result is as follows:

It can be found that, without any parameter input, the array argv is not empty, but contains a parameter, that is, the path and file name of the current execution file. This executable file is stored in argv [0.

Next, use the vs2005 project properties-> debugging-> command parameters to set the input parameter "I am gecko" and continue to run the program.

The result is shown in the following figure.

That is, the input parameter is filled from argv [1], and argv [0] is still the path and file name of the stored execution file.

The problem will also occur here. If you use vs2005 to debug the program and do not set parameters in the command parameters, an error will be reported if you access argv [1.

In the above case, the biggest possibility is to operate the NULL pointer. (In the past, we often encountered such a problem, which made us confused)

In this case, you can first check the number of parameters when operating the pointer.

// If you want to access argv [1] and argv [2], you can check if (argc! = 3) {return 1 ;}

In fact: int main (INT argc, char * argv []) is a standard writing method in UNIX and Linux, while int main () is only a default usage in UNIX and Linux ..

The following is an example of Edit. C.

# Include <unistd. h> # include <stdio. h> int main (INT argc, char * argv []) {If (argc = 1 | argc> 2) {printf ("Enter the file name you want to edit, for example :. /edit fillen ");} If (argc = 2) {printf (" Edit % Sn ", argv [1]);} exit (0 )}

Compile the program: gcc-O edit. c
Run:./edit
Result: Enter the file name to edit, for example,./edit fille.
Run:./edit edit.txt
Result: Edit edit.txt.

Here we can see how argc and argv [] are used. argc is the number of external command parameters. argv [] stores the content of each parameter, as shown in the following example: execution. /edit, argc is 1,
When argv [0] is./edit. and execution of./edit edit.txt, the value of argc is 2,
Argv [0] is./edit, argv [1] is edit.txt.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.