当前位置:网站首页>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]
边栏推荐
- CSDN salary increase technology - learn about the use of several common logic controllers of JMeter
- Use 3 in data modeling σ Eliminate outliers for data cleaning
- Final keyword
- NETCORE 3.1 solves cross domain problems
- Network request process
- 章鱼未来之星获得25万美金奖励|章鱼加速器2022夏季创业营圆满落幕
- sql 里面使用中文字符判断有问题,哪位遇到过?比如value&lt;&gt;`无`
- Kubernetes cluster capacity expansion to add node nodes
- 第一讲:寻找矩阵的极小值
- The configuration and options of save actions are explained in detail, and you won't be confused after reading it
猜你喜欢
# Arthas 简单使用说明
细说Mysql MVCC多版本控制
Unity3d interface is embedded in WPF interface (mouse and keyboard can respond normally)
Use 3 in data modeling σ Eliminate outliers for data cleaning
沙龙预告|GameFi 领域的瓶颈和解决方案
Sqlplus garbled code problem, find the solution
Netease cloud wechat applet
JMeter JDBC batch references data as input parameters (the simplest method for the whole website)
Install pyqt5 and Matplotlib module
The configuration and options of save actions are explained in detail, and you won't be confused after reading it
随机推荐
Unity shader (data type in cghlsl)
Detailed explanation of diffusion model
【原创】程序员团队管理的核心是什么?
Octopus future star won a reward of 250000 US dollars | Octopus accelerator 2022 summer entrepreneurship camp came to a successful conclusion
2020浙江省赛
面试被问到了解哪些开发模型?看这一篇就够了
Esp8266 uses TF card and reads and writes data (based on Arduino)
请教个问题,我用sql-client起了个同步任务,从MySQL同步到ADB,历史数据有正常同步过去
Netease cloud wechat applet
Over 100000 words_ Ultra detailed SSM integration practice_ Manually implement permission management
进程间的通信方式
数据库多表关联查询问题
[Frida practice] "one line" code teaches you to obtain all Lua scripts in wegame platform
CSDN salary increase technology - learn about the use of several common logic controllers of JMeter
Lecture 1: stack containing min function
flink. CDC sqlserver. 可以再次写入sqlserver中么 有连接器的 dem
基础篇:带你从头到尾玩转注解
Binary tree high frequency question type
Information Security Experiment 2: using x-scanner scanning tool
H5网页播放器EasyPlayer.js如何实现直播视频实时录像?