当前位置:网站首页>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;
}边栏推荐
- Raki's notes on reading paper: discontinuous named entity recognition as maximum clique discovery
- Pytorch学习(二)
- shell统计某个字符串最后一次出现的位置之前的所有字符串
- Federal learning: dividing non IID samples by Dirichlet distribution
- Global and Chinese markets of liquid optical waveguides 2022-2028: Research Report on technology, participants, trends, market size and share
- Several key points recorded after reviewing redis design and Implementation
- SQLite use
- Some configuration details about servlet initial development
- Two methods of SSL certificate format conversion
- CMake教程系列-05-选项及变量
猜你喜欢

GTK interface programming (I): Environment Construction

Welfare lottery | what are the highlights of open source enterprise monitoring zabbix6.0

O & M (21) make winpe startup USB flash disk

What is the difference between a layer 3 switch and a layer 2 switch
![[dry goods sharing] the latest WHQL logo certification application process](/img/c3/37277572c70b0af944e594f0965a6c.png)
[dry goods sharing] the latest WHQL logo certification application process

Série de tutoriels cmake - 02 - génération de binaires à l'aide du Code cmake

Unity TimeLine 数据绑定

Two methods of SSL certificate format conversion

公司电脑强制休眠的3种解决方案

Some configuration details about servlet initial development
随机推荐
Jvxetable sub table record loading completion event
List of development tools
Hands on in-depth learning notes (XV) 4.1 Multilayer perceptron
Implementation of Sanzi chess with C language
Study diary: February 15, 2022
Which is a good foreign exchange trading platform? Is it safe to have regulated funds?
多卡服务器使用
What is certificate transparency CT? How to query CT logs certificate logs?
Distributed file storage system fastdfs hands on how to do it
Global and Chinese market of ERP software for garment and textile industries 2022-2028: Research Report on technology, participants, trends, market size and share
Comparable和Comparator的区别
NLP text summary: data set introduction and preprocessing [New York Times annotated corpus]
怎么利用Redis实现点赞功能
What is a self signed certificate? Advantages and disadvantages of self signed SSL certificates?
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
外汇交易平台哪个好?有监管的资金就安全吗?
模板参数包和函数参数包
Raki's notes on reading paper: discontinuous named entity recognition as maximum clique discovery
Federal learning: dividing non IID samples by Dirichlet distribution
Redis+AOP怎么自定义注解实现限流