当前位置:网站首页>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]
边栏推荐
- How will fashion brands enter the meta universe?
- csdn涨薪技术-浅学Jmeter的几个常用的逻辑控制器使用
- CDZSC_2022寒假个人训练赛21级(1)
- 在EXCEL写VBA连接ORACLE并查询数据库中的内容
- flex弹性布局
- Vs2013 generate solutions super slow solutions
- Sqlplus garbled code problem, find the solution
- Dynamics 365online applicationuser creation method change
- 根据热门面试题分析Android事件分发机制(二)---事件冲突分析处理
- Unity shader (learn more about vertex fragment shaders)
猜你喜欢

Unity uses mesh to realize real-time point cloud (I)

章鱼未来之星获得25万美金奖励|章鱼加速器2022夏季创业营圆满落幕

# Arthas 简单使用说明

Impression notes finally support the default markdown preview mode

What development models did you know during the interview? Just read this one

细说Mysql MVCC多版本控制

信息安全实验一:DES加密算法的实现

PLC信号处理系列之开关量信号防抖FB

iNFTnews | 时尚品牌将以什么方式进入元宇宙?

Lecture 1: stack containing min function
随机推荐
How to become a senior digital IC Design Engineer (5-2) theory: ULP low power design technology (Part 1)
[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
高斯消元
[Frida practice] "one line" code teaches you to obtain all Lua scripts in wegame platform
软件建模与分析
ViewPager2和VIewPager的區別以及ViewPager2實現輪播圖
nlohmann json
Difference between process and thread
第十四次试验
Detailed explanation of diffusion model
Create an int type array with a length of 6. The values of the array elements are required to be between 1-30 and are assigned randomly. At the same time, the values of the required elements are diffe
Liunx command
第一讲:鸡蛋的硬度
如何成为一名高级数字 IC 设计工程师(5-2)理论篇:ULP 低功耗设计技术精讲(上)
章鱼未来之星获得25万美金奖励|章鱼加速器2022夏季创业营圆满落幕
用flinksql的方式 写进 sr的表,发现需要删除的数据没有删除,参照文档https://do
Vs2013 generate solutions super slow solutions
Netease Cloud Wechat applet
2020浙江省赛
Niuke - Huawei question bank (61~70)