当前位置:网站首页>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]
边栏推荐
- JMeter JDBC batch references data as input parameters (the simplest method for the whole website)
- flinkcdc 用sqlclient可以指定mysqlbinlog id执行任务吗
- Gym - 102219J Kitchen Plates(暴力或拓扑序列)
- JS reverse tutorial second issue - Ape anthropology first question
- How will fashion brands enter the meta universe?
- 牛客网——华为题库(61~70)
- Arthas simple instructions
- Upload taro pictures to Base64
- Impression notes finally support the default markdown preview mode
- 细说Mysql MVCC多版本控制
猜你喜欢

nlohmann json

信息安全实验三 :PGP邮件加密软件的使用

Binary tree high frequency question type
![[4g/5g/6g topic foundation-146]: Interpretation of white paper on 6G overall vision and potential key technologies-1-overall vision](/img/fd/5e8f74da25d9c5f7bd69dd1cfdcd61.png)
[4g/5g/6g topic foundation-146]: Interpretation of white paper on 6G overall vision and potential key technologies-1-overall vision

小程序实现页面多级来回切换支持滑动和点击操作

软件建模与分析

数据建模中利用3σ剔除异常值进行数据清洗

第一讲:包含min函数的栈

细说Mysql MVCC多版本控制

Unity3d interface is embedded in WPF interface (mouse and keyboard can respond normally)
随机推荐
JS judge whether checkbox is selected in the project
MongoDB怎么实现创建删除数据库、创建删除表、数据增删改查
Difference between process and thread
Lesson 1: hardness of eggs
IIS faked death this morning, various troubleshooting, has been solved
Octopus future star won a reward of 250000 US dollars | Octopus accelerator 2022 summer entrepreneurship camp came to a successful conclusion
Communication mode between processes
数据库多表关联查询问题
thinkphp数据库的增删改查
细说Mysql MVCC多版本控制
Unity shader (data type in cghlsl)
esp8266使用TF卡并读写数据(基于arduino)
js逆向教程第二发-猿人学第一题
第十四次试验
哈夫曼编码压缩文件
[4g/5g/6g topic foundation-146]: Interpretation of white paper on 6G overall vision and potential key technologies-1-overall vision
Arthas simple instructions
NATAPP内网穿透
The difference between viewpager2 and viewpager and the implementation of viewpager2 in the rotation chart
CSDN salary increase technology - learn about the use of several common logic controllers of JMeter