当前位置:网站首页>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]
边栏推荐
- Install pyqt5 and Matplotlib module
- How will fashion brands enter the meta universe?
- 洛谷P2482 [SDOI2010]猪国杀
- [bw16 application] Anxin can realize mqtt communication with bw16 module / development board at instruction
- js逆向教程第二发-猿人学第一题
- NETCORE 3.1 solves cross domain problems
- 有没有大佬帮忙看看这个报错,有啥排查思路,oracle cdc 2.2.1 flink 1.14.4
- Netease cloud wechat applet
- iNFTnews | 时尚品牌将以什么方式进入元宇宙?
- [Frida practice] "one line" code teaches you to obtain all Lua scripts in wegame platform
猜你喜欢
Diffusion模型详解
Netease cloud wechat applet
Lesson 1: finding the minimum of a matrix
How does mongodb realize the creation and deletion of databases, the creation of deletion tables, and the addition, deletion, modification and query of data
Kubernetes cluster capacity expansion to add node nodes
信息安全实验三 :PGP邮件加密软件的使用
VSCode+mingw64+cmake
Oracle installation enhancements error
【原创】程序员团队管理的核心是什么?
如何使用clipboard.js库实现复制剪切功能
随机推荐
thinkphp数据库的增删改查
基于智慧城市与储住分离数字家居模式垃圾处理方法
La différence entre viewpager 2 et viewpager et la mise en œuvre de la rotation viewpager 2
Binary tree high frequency question type
liunx命令
Kubernetes cluster capacity expansion to add node nodes
基础篇:带你从头到尾玩转注解
NATAPP内网穿透
如何使用clipboard.js库实现复制剪切功能
[4g/5g/6g topic foundation -147]: Interpretation of the white paper on 6G's overall vision and potential key technologies -2-6g's macro driving force for development
剑指 Offer II 107. 矩阵中的距离
Information Security Experiment 4: implementation of IP packet monitoring program
How to become a senior digital IC Design Engineer (5-3) theory: ULP low power design technology (Part 2)
JS judge whether checkbox is selected in the project
How to become a senior digital IC Design Engineer (5-2) theory: ULP low power design technology (Part 1)
[4G/5G/6G专题基础-146]: 6G总体愿景与潜在关键技术白皮书解读-1-总体愿景
CDZSC_2022寒假个人训练赛21级(1)
网易云微信小程序
第一讲:包含min函数的栈
有没有大佬帮忙看看这个报错,有啥排查思路,oracle cdc 2.2.1 flink 1.14.4