当前位置:网站首页>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;
}
边栏推荐
- Precautions for purchasing wildcard SSL certificate
- Azure developer news flash list of events of developers in June
- LeetCode 3. 无重复字符的最长子串
- New edition of diazotization process in 2022 and analysis of diazotization process
- [Postgres] Postgres database migration
- Customize the buttons of jvxetable and the usage of $set under notes
- Distributed file system fastdfs
- CMake教程系列-02-使用cmake代码生成二进制
- [oiclass] chess piece
- 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
猜你喜欢
Software testing skills, JMeter stress testing tutorial, transaction controller of logic controller (25)
What should academic presentation /ppt do?
High paid programmers & interview questions series 63: talk about the differences between sleep (), yield (), join (), and wait ()
Heavy attack -- ue5's open source digital twin solution
Distributed file system fastdfs
Pytorch学习(二)
Precautions for purchasing wildcard SSL certificate
2. successfully solved bug:exception when publishing [Failed to connect and initialize SSH connection...
重磅来袭--UE5的开源数字孪生解决方案
Raki's notes on reading paper: discontinuous named entity recognition as maximum clique discovery
随机推荐
[dry goods sharing] the latest WHQL logo certification application process
Differences among digicert, SECTIONO and globalsign code signing certificates
How to switch ipykernel to a different CONDA virtual environment in jupyterlab?
FDA mail security solution
FAQs for code signature and driver signature
Global and Chinese markets for wireless security in LTE networks 2022-2028: Research Report on technology, participants, trends, market size and share
JMeter obtains cookies across thread groups or JMeter thread groups share cookies
The rigorous judgment of ID number is accurate to the last place in the team
What is an X.509 certificate? 10. 509 certificate working principle and application?
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
What is the concept of string in PHP
Mysql提取表字段中的字符串
Network neuroscience -- a review of network Neuroscience
Federal learning: dividing non IID samples by Dirichlet distribution
Several key points recorded after reviewing redis design and Implementation
Cmake tutorial series -04- compiling related functions
Raki's notes on reading paper: Leveraging type descriptions for zero shot named entity recognition and classification
迅为恩智浦iTOP-IMX6开发平台
What files does a CA digital certificate contain? How to view SSL certificate information?
怎么利用Redis实现点赞功能