当前位置:网站首页>Formal and actual parameters, value passing and address passing
Formal and actual parameters, value passing and address passing
2022-06-30 02:56:00 【@sen】
Shape parameter : Formal parameters , It can be understood by the formal subject in English ;
Actual parameters : The actual parameter , It can be understood by the actual subject in English ;
int sum(int n) // Formal parameters
{
int result = 0;
do
{
result += n;
} while (n-- > 0);
return result;
}
int main()
{
result = sum(n); // The actual parameter
}Pass value : The passed in variable does not contain a pointer , In variables that do not contain pointers , Arguments do not affect main The value of the inside , stay c Each function in the language has its own scope , The interior is not interlinked
#include<stdio.h>
void swap(int x, int y); //swap: The meaning of exchange , Exchange with each other , No need to return
void swap(int x, int y)
{
int temp;
printf("in swap, Before switching :x=%d,y=%d\n", x, y);
temp = x;
x = y;
y = temp;
printf("in swap, After exchange :x=%d,y=%d\n", x, y);
}
int main()
{
int x = 3, y = 5;
printf("in main, Before switching :x=%d,y=%d\n", x, y);
swap(x, y);
printf("in main, After exchange :x=%d,y=%d\n", x, y);
return 0;
}Code above ,x and y Value , stay swap There are changes before and after the exchange of functions , But it doesn't affect main In the function x and y Value
Byref : Variables with pointers , The pointer holds the address , Only the address needs to be passed between the two functions , You can affect the values of different functions , Equivalent to an arbitrary gate
#include<stdio.h>
void swap(int *x, int *y);
void swap(int *x, int *y)
{
int temp;
printf("in swap, Before switching :x=%d,y=%d\n", *x, *y);
temp = *x;
*x = *y;
*y = temp;
printf("in swap, After exchange :x=%d,y=%d\n", *x, *y);
}
int main()
{
int x = 3, y = 5;
printf("in main, Before switching :x=%d,y=%d\n", x, y);
swap(&x, &y);
printf("in main, After exchange :x=%d,y=%d\n", x, y);
return 0;
}Code above , Put the corresponding x,y prefix *, It turns a variable into a pointer , At this point, the values of the two functions interact with each other , So in swap Function x,y After exchange ,main Medium x,y The value has changed accordingly
Pass array : Not all parameters will be passed in , In fact, only the address of the first element is passed
#include<stdio.h>
void get_array(int a[10]); //get_array Array name
void get_array(int a[10])
{
int i;
a[5] = 520;
for (i = 0; i < 10; i++)
{
printf(" a[%d]=%d\n",i,a[i]);
}
}
int main()
{
int a[10] = { 1,2,3,4,5,6,7,8,9,0 };
int i;
printf(" stay main Function to print again \n");
for (i = 0; i < 10; i++)
{
printf(" a[%d]=%d", i, a[i]);
}
get_array(a);
return 0;
}If you are not clear, look at the following example
because void get_array Medium b It's just an address , So only 4 Bytes , and main Function a It's a plastic variable , Each of these elements accounts for 4 Bytes , So the total is 4*10=40 Bytes
#include<stdio.h>
void get_array(int b[10]);
void get_array(int b[10])
{
printf("sizeof b:%d\n", sizeof(b));
}
int main()
{
int a[10] = { 1,2,3,4,5,6,7,8,9,0 };
printf("sizeof a:%d\n", sizeof(a));
get_array(a);
return 0;
}边栏推荐
- threejs 镜子案例Reflector 创建镜子+房子搭建+小球移动
- 2022 new test questions for safety management personnel of metal and nonmetal mines (small open pit quarries) and certificate examination for safety management personnel of metal and nonmetal mines (s
- Several key points recorded after reviewing redis design and Implementation
- 迅為恩智浦iTOP-IMX6開發平臺
- 怎样的外汇交易平台是有监管的,是安全的?
- What is a self signed certificate? Advantages and disadvantages of self signed SSL certificates?
- List of development tools
- Cmake tutorial series -02- generating binaries using cmake code
- Hands on in-depth learning notes (XV) 4.1 Multilayer perceptron
- Raki's notes on reading paper: Leveraging type descriptions for zero shot named entity recognition and classification
猜你喜欢

Study diary: February 15, 2022

可视化HTA窗体设计器-HtaMaker 界面介绍及使用方法,下载 | HTA VBS可视化脚本编写

Detailed explanation of minimum stack

FDA ESG regulation: digital certificate must be used to ensure communication security

Mysql表数据比较大情况下怎么修改添加字段

并发请求下如何防重复提交

Raki's notes on reading paper: neighborhood matching network for entity alignment

微信小程序页面跳转以及参数传递

重磅来袭--UE5的开源数字孪生解决方案

Enlightenment from the revocation of Russian digital certificate by mainstream CA: upgrade the SSL certificate of state secret algorithm to help China's network security to be autonomous and controlla
随机推荐
How to switch ipykernel to a different CONDA virtual environment in jupyterlab?
Implementation of Sanzi chess with C language
O & M (21) make winpe startup USB flash disk
Idea remote debugging remote JVM debug
CMake教程系列-05-选项及变量
Differences among digicert, SECTIONO and globalsign code signing certificates
How to use redis to realize the like function
Raki's notes on reading paper: discontinuous named entity recognition as maximum clique discovery
Federal learning: dividing non IID samples by Dirichlet distribution
unity的text首列有标点符号咋办
Note the use of export/import and class inheritance in ES6
[Postgres] Postgres database migration
Heavy attack -- ue5's open source digital twin solution
HTA introductory basic tutorial | GUI interface of vbs script HTA concise tutorial, with complete course and interface beautification
2. < tag dynamic programming and 0-1 knapsack problem > lt.416 Split equal sum subset + lt.1049 Weight of the last stone II
Enlightenment from the revocation of Russian digital certificate by mainstream CA: upgrade the SSL certificate of state secret algorithm to help China's network security to be autonomous and controlla
Time complexity analysis
Linear algebra Chapter 4 Summary of knowledge points of linear equations (Jeff's self perception)
Summary of knowledge points about eigenvalues and eigenvectors of matrices in Chapter 5 of Linear Algebra (Jeff's self perception)
LeetCode 3. 无重复字符的最长子串