当前位置:网站首页>Main (argc, *argv[]) details

Main (argc, *argv[]) details

2022-07-07 09:45:00 Don't ask me y

We often use main Functions have no arguments . therefore main The brackets after are empty . actually ,main Functions can take arguments , This parameter can be taken as main The formal parameter of the function .C Language policy main There can only be two arguments to a function , Traditionally, these two parameters are written as argc and argv. therefore ,main The function header of a function can be written as : main (argc,argv)C The language also stipulates argc( First parameter ) Must be an integer variable ,argv( The second parameter ) Must be an array of pointers to a string . After adding the formal parameter description ,main The function header of a function should be written as :
main (argc,argv)
int argv;
char *argv[]; Or written as :
main (int argc,char *argv[])
   because main Function cannot be called by other functions , Therefore, it is impossible to obtain the actual value inside the program . that , Where to assign an argument value to main The formal parameters of the function ? actually ,main The parameter values of the function are obtained from the operating system command line . When we want to run an executable , stay DOS Type the file name at the prompt , Then enter the actual parameters to transfer them to main In the formal parameters of .

  DOS The general form of the command line at the prompt is : C:/> Executable file name Parameters Parameters ……; But special attention should be paid to ,main The two formal parameters of and the parameters in the command line are
The position is not one-to-one . because ,main There are only two formal parameters , The number of parameters in the command line is not limited in principle .argc Parameter indicates the number of parameters in the command line ( Be careful : The file name itself is also a parameter ),argc The value of is automatically assigned by the system according to the number of actual parameters when entering the command line . For example, there is command behavior : C:/>E6 24 BASIC dbase FORTRAN Due to the file name E6 24 It is also a parameter , So there is 4 Parameters , therefore argc The value obtained is 4.argv The parameter is a string pointer array , Its element values are strings in the command line ( Parameters are treated as strings ) The first address . The length of the pointer array is the number of parameters . The initial values of array elements are automatically assigned by the system . Its representation is shown in the figure 6.8 Shown :
main(int argc,char *argv[]){
while(argc-->=1)
printf("%s/n",* argv+4-argc);
}
This example shows the parameters entered in the command line. If the executable file name in the above example is e24.exe, Store in A Inside the drive .
So the input command behavior : C:/>a:e24 BASIC dBASE FORTRAN
Then the running result is :
BASIC
dBASE
FORTRAN
   The bank has 4 Parameters , perform main when ,argc The initial value of is 4.argv Of 4 Each element is divided into 4 The first address of a string . perform while sentence , Every cycle argv Value reduction 1, When argv be equal to 1 Stop the cycle when , Three times in total , Therefore, three parameters can be output . stay printf Function , Because the print item * argv It's to add... First 1 Re print , So the first print is argv[1] The string in question BASIC. second 、 Print the last two strings in three cycles . And the parameters e24 File name , No output required .

   There are two parameters in the command line of the following example , The second parameter 20 Is the entered n value . In the program * argv The value of is string “20”, And then use the function "atoi" Replace it with an integer as while Loop control variables in statements , Output 20 An even number .
#include"stdlib.h"
main(int argc,char*argv[]){
int a=1,n;
n=atoi(* argv);
while(n--) printf("%d ",a *2);
}
   This procedure is from 2 Start to output n An even number . Pointer variable pointing to pointer if one pointer variable stores the address of another pointer variable , The pointer variable is called the pointer variable to the pointer .

   I have already introduced , Accessing variables through pointers is called indirect access , Short for inter visit . Because pointer variables point directly to variables , So it is called single level inter visit . And if the variable is accessed through the pointer variable pointing to the pointer, it constitutes a two-level or multi-level inter access . stay C Language program , The number of visits is not clearly Limited , But it is not easy to understand the solution when there are too many interlocution series , It's also easy to make mistakes , therefore , Generally, it is rarely more than level 2 inter visit . The general form of pointer variable description pointing to pointer is :
Type specifier ** Pointer variable name ;
for example : int ** pp; Express pp Is a pointer variable , It points to another pointer variable , And this pointer variable points to an integer . Here is an example to illustrate this relationship .
main(){
int x,*p,**pp;
x=10;
p=&x;
pp=&p;
printf("x=%d/n",**pp);
}
   In the above example program p Is a pointer variable , Point to integer quantity x;pp It's also a pointer variable , It points to a pointer variable p. adopt pp Variable access x The way to write is **pp. The program finally outputs x The value of is 10. Through the example above , Readers can learn the description and usage of pointer variables pointing to pointers .

   The following program first defines and describes the pointer array ps And the initialization assignment is made . It also explains pps Is a pointer variable that points to a pointer . stay 5 In the secondary cycle , pps Respectively obtained ps[0],ps[1],ps[2],ps[3],ps[4] Address value of ( Pictured 6.10 Shown ). Then you can find the string through these addresses .
main(){
static char *ps[]={ "BASIC","DBASE","C","FORTRAN",
"PASCAL"};
char **pps;
int i;
for(i=0;i <5;i ){
pps=ps i;
printf("%s/n",*pps);
}
}
This program is programmed with pointer variables that point to pointers , Output multiple strings

Reply from netizens : When I first came into contact with these two variables , I have no idea what they are used for , I think many people are like me , When I first saw these two variables, I was also confused .

Actually : int main(int argc,char *argv[]) yes UNIX and Linux The standard way of writing in , and int main() It's just UNIX And Linux The usage of acquiescence ..

What the hell is that argc,argv[] What's the use ? Here's an example edit.c You will understand their usage :

#include <unistd.h>
#include <stdio.h>

int main(int argc,char *argv[])
{
if(argc==1 || argc>2) {
printf(" Please enter the file name you want to edit, such as :./edit fillen");
}
if(argc==2) {
printf(" edit %sn",argv[1]);
}
exit(0)
}

Compile the program :gcc -o edit edit.c
function :./edit
result : Please enter the file name you want to edit, such as :./edit fille
function :./edit edit.txt
result : edit edit.txt

See here argc,argv[] How to use it is very clear ,argc Is the number of external command parameters ,argv[] Store the contents of each parameter , As in the above example : perform ./edit when ,argc by 1,
argv[0] by ./edit . And perform ./edit edit.txt when ,argc The value of is 2,
argv[0] by ./edit,argv[1] by edit.txt .
Reply from netizens : To put it simply , That's the command line argument !

argc Is the number of external command parameters ,argv[] Store the contents of each parameter , As in the above example : perform ./edit when ,argc by 1,
argv[0] by ./edit . And perform ./edit edit.txt when ,argc The value of is 2,
argv[0] by ./edit,argv[1] by edit.txt .
Reply from netizens :int argc Number of parameters
char** argv Parameters of the array ,, The array size is the previous parameter , Access the contents stored inside through the array subscript , for example argv[0],argv[1]

原网站

版权声明
本文为[Don't ask me y]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202130624178720.html