当前位置:网站首页>Formal parameters, arguments, main function parameters, arrays or pointers as function parameters of the knowledge in every corner of C language
Formal parameters, arguments, main function parameters, arrays or pointers as function parameters of the knowledge in every corner of C language
2022-07-29 01:30:00 【Listen to music with bars_ 0】
Catalog
Definition and relation of formal parameter and argument
Relationship between formal parameters and arguments :
A one-dimensional array is used as the shape of a function to participate in the arguments
A one-dimensional array is used as the shape of a function to participate in the arguments
Array as function parameter ( In the form of a pointer )
main Function parameter problem
Definition and relation of formal parameter and argument
Formal parameters :
When defining a function , Specified parameters . A parameter that appears in a function definition can be seen as a placeholder , It has no data , You can only wait until the function is called to receive the data passed in , So called Formal parameters , abbreviation Shape parameter .
Formal parameter variables are only available in functions Memory is allocated only when called , After call , Free memory now , So formal parameter variables are only valid inside the function , Cannot use... Outside a function .
The actual parameter :
When you call a function , Specified parameters . The parameters given when the function is called contain real data , Will be used by the code inside the function , So called The actual parameter , abbreviation Actual parameters .
Arguments can be constant 、 Variable 、 expression 、 Functions, etc , No matter what type of data the argument is , When you make a function call , Both of them There must be a definite value , In order to pass these values to the parameter , So we should use assignment in advance 、 Input and so on so that the argument gets a definite value .
Relationship between formal parameters and arguments :
The function of formal parameters and arguments is to pass data , When a function call occurs , The value of the argument is passed to the formal parameter .
Array as function parameter
A one-dimensional array is used as the shape of a function to participate in the arguments
int a[10];
void function(int a[],int n) // Formal parameters
function(a,10) // The actual parameter
A one-dimensional array is used as the shape of a function to participate in the arguments
int a[5][10];
void function(int a[][10],int n) // Formal parameters
function(a,5) // The actual parameter Array as function parameter ( In the form of a pointer )
Some students will have questions , Didn't the above just say that arrays are used as function parameters , Why is there another one in the form of a pointer . Don't worry , We all know that an array is also a pointer ( That's the address ), So arrays can also be represented in the form of pointers , I put this part here , It is just used as an array to buffer function parameters and a pointer to buffer function parameters
Let's directly use an example to compare the difference between the two forms when arrays are used as function parameters , To find the maximum value in a one-dimensional array
/*
find_max: Take the form of an array as a parameter to find the maximum value of a one-dimensional array
@a[]: One dimensional array that needs to be maximized
@n: Number of elements of one-dimensional array
Return value
max: The maximum value of one-dimensional array
*/
int find_max(int a[],int n)
{
int i;
int max = a[0];
for(i = 0; i < n; i++)
{
if(a[i] > max)
{
max = a[i];
}
}
return max;
}/*
find_max: Array takes the form of pointer as a parameter to find the maximum value of one-dimensional array
@a[]: One dimensional array that needs to be maximized
@n: Number of elements of one-dimensional array
Return value
max: The maximum value of one-dimensional array
*/
int find_max(int *b,int n)
{
int i;
int max = *b; //*b = *&b[0]= b[0]
for(i = 0; i < n; i++)
{
if(*(b+i) > max)
{
max = *(b+i);
}
}
return max;
}Add some difficulty , It is also to make everyone better understand , Let's input and output a two-dimensional array with an array as a pointer
void _3()
{
int a[3][3];
for(int i = 0;i<3;i++)
{
for(int j = 0;j < 3;j++)
{
scanf("%d",(*(a+i)+j)); // Input of two-dimensional array , Equivalent to scanf("%d",a[i][j]);
}
}
for(int i = 0;i<3;i++)
{
for(int j = 0;j < 3;j++)
{
printf("%d ",*(*(a+i)+j)); // Output of two-dimensional array , Equivalent to printf("%d ",a[i][j]);
}
printf("\n");
}
printf("\n");
}Pointer as function parameter
/*
_17: Use pointers as arguments , Exchange two values
*/
void _17(int *p,int *q)
{
int t;
t = *p;
*p = *q;
*q = t;
}
int main()
{
int a = 0,b = 1;
_17(&a,&b);
printf("a = %d, b = %d\n",a,b);
}void func(int *p)
{
*p = 250;
}
int main()
{
int a;
func(&a);//p = &a;
//typeof(p): int *
//typeof(&a): typeof(a)* int *
printf("%d\n",a);
}main Function parameter problem
Talking about main function , We must all be able to play quickly “int main(){}" Such sentences , however main Function actually has three optional parameters , as follows
int main(int argc, char *argv[], char *envp[]);
argc: It's a int type , Represents the number of parameters on the command line
argv: char*[], Is an array of string pointers .
argv[0]: Save the address of the first parameter string of the program
argv[1]: Save the address of the second parameter string of the program
....
argv[argc] by NULL , Indicates the end of the parameter .
char* envp[], It's also an array of strings , It is mainly to save the variable string in the user environment , With NULL end .
according to C The rules of language ,argv[0] The value of is the name of the program that started the program .
eq:./a.out ./hello
So agrc The value of is at least one , if agrc The value of is 1, It indicates that there is no command line parameter after the program name .
Let's try to input the command line parameters on the terminal , Then print it out at the terminal . Also test the third parameter
int main(int argc,char *argv[]) // Print the command input by the terminal
{
int i = 0;
while(argv[i++])
{
printf("%s ",argv[i]);
}
printf("\n");
i = 0;
while(envp[i++])
{
printf("%s\n", envp[i]);
}
return 0;
}Excellent article in the past :
边栏推荐
- [ManageEngine] help Harbin Engineering University realize integrated monitoring and management of network traffic
- RHCE command practice (II)
- Subtotal of process thread coordination
- Date conversion EEE MMM DD hh:mm:ss zzz YYYY
- Naver 三方登录
- Letax record \documentclass{}, authoryear attribute is used
- RHCE命令练习(二)
- Rewriting method set
- 云原生应用综合练习下
- vm options、program arguments、environment property
猜你喜欢

Use of resttemplate and Eureka

Openpyxl merge cells
![[MySQL] historical cumulative de duplication of multiple indicators](/img/22/1d5162e2c9636c9455138f2f39d150.png)
[MySQL] historical cumulative de duplication of multiple indicators

Oozie工作调度

C language 300 lines of code to achieve mine sweeping (deployable + markable + changeable difficulty level)

跨模态对齐 20220728

了解网址url的组成后 运用url模块、querystring模块和mime模块完善静态网站

梅克尔工作室——HarmonyOS实现列表待办

地下水、土壤、地质、环境人看过来

nep 2022 cat
随机推荐
拼多多众多 API 接口皆可使用
Understand all the basic grammar of ES6 in one article
IT硬件故障的主要原因和预防的最佳实践
ActiveMQ basic details
regular expression
Synchronized关键字详解
Flask project architecture (First Edition
APP接入Kakaotalk三方登录
Oozie job scheduling
LeTax记录\documentclass{},authoryear属性使用
时间复杂度、空间复杂度的学习总结
[MySQL] string to int
Teach you a text to solve the problem of JS digital accuracy loss
Django reports an error using pymsql module django.db.utils.operationalerror
SDRAM Controller Design (two design methods of digital controller)
正则校验与时间格式化
Groundwater, soil, geology and environment
RHCE命令练习(一)
CSDN modify column name
Use of resttemplate and Eureka