当前位置:网站首页>Parameter passing mode of C language (int x) (int *x) (int & x)
Parameter passing mode of C language (int x) (int *x) (int & x)
2022-07-29 09:26:00 【A programmer who loves playing badminton】
I insist on updating C Language , data structure , operating system , Front end knowledge , Can collect + Follow and keep abreast
Catalog
C There are three ways of transmitting parameters in language
C There are three ways of transmitting parameters in language
1. Transfer value (int x)
void test(int x){
} Is to pass the value of your variable
Formal parameters to function , In fact, a new formal parameter is generated with the value of the variable , Therefore, the change of parameters in the function will not affect the value of variables outside the function .
Be careful : Formal and actual parameters are never a variable
The most classic example is to exchange the values of two numbers , When a function is called , When an argument is passed to a formal parameter , The formal parameter will be a temporary copy of the argument , Therefore, the modification of formal parameters does not affect the arguments , If you pass parameters in this way , Just exchange the values of formal parameters , Will not really exchange the values of two numbers
void exchange(int a, int b)
{
int t;// Define intermediate variables
t = a;
a = b;
b = temp;
}
int main(int argc, char const *argv[])
{
int a = 3;
int b = 5;
exchange(a, b);
printf("----------------------\n");
printf("a=%d\n", a);
printf("b=%d\n", b);
return 0;
}2. Address (int *x)
Is to pass the address of the variable to the pointer of the formal parameter in the function , Make the pointer point to the address of the real variable , Because the change of the content of the address indicated by the pointer can be reflected outside the function , That is, it can change the value of variables outside the function .
void exchange_3(int *p, int *q)
{
int t;
// If you want to swap *p and *q Value , be t Must be defined as int, Cannot be defined as int*, Otherwise syntax error
t = *p; //p The data type of is int*,*p The data type of is int
*p = *q;
*q = t;
}
int main(int argc, char const *argv[])
{
int a = 3;
int b = 5;
exchange_3(&a, &b);
printf("----------------------\n");
printf("a=%d\n", a);
printf("b=%d\n", b);
return 0;
}But here's the thing , The size of pointer variables always only accounts for 4 Bytes , So if you pass it with a pointer , Will be faster , It's more efficient .
3. The reference (int &x)
Quote actually Is an alias of a variable , Has the same memory space as this variable .
Arguments pass variables to formal parameter references , amount to A formal parameter is an alias for an argument variable , The modification of formal parameters is to modify the arguments directly .
Objects are often used as members of the referenced class , Greatly improve the efficiency of the code
For example, when we initialize a stack
int IninStack(SqStack &S)
{
S.base = (int *)malloc(STACKSIZEINIT * sizeof(int));
if (!S.base)
{
exit(-1); // memory allocation failed , Exit procedure
}
S.top = S.base; // The top stack pointer and the bottom stack pointer refer to the same storage unit
S.stacksize = STACKSIZEINIT;
return 1;
}summary
Declare a reference , It's not a new variable , It only means that the reference name is an alias of the target variable name , It is not a data type in itself , Therefore, the reference itself does not occupy the storage unit , The system also does not allocate storage units to references . so : Address a reference , It is to find the address of the target variable .&ra And &a equal .
Reference passing can be regarded as based on value passing , Add a... Before the formal parameter variables defined and declared by the function &, Other uses are exactly the same as value passing , Therefore, it can be seen that reference passing is more convenient
边栏推荐
- 23考研人撑住!考研第一波弃考高峰期已经到来!
- C # use restsharp library to realize post request
- Network knowledge summary
- 基于C语言实现的NFA确定化和DFA最小化
- First order traversal / second order traversal determines the approximate shape of the tree
- 如何为OpenHarmony做贡献
- 40余岁的边缘老技术,是未来乏力还是掘地而起?
- 简述堆和栈的区别
- 浅谈契约测试
- Solve the problem of reading data garbled by redis visualization tool
猜你喜欢
随机推荐
redis可视化工具读取数据乱码问题解决
Quaternion and its simple application in unity
[machine learning] logistic regression code exercise
如何介绍自己的项目经验?
dataframe. to_ Sql() inserts too many errors at one time
基于C语言模拟实现DFA识别字符串
存算一体与存内计算计算杂谈
36. JS动画
【BERT-多标签文本分类实战】之一——实战项目总览
No swagger, what do I use?
Asp graduation project - based on C # +asp Net+sqlserver laboratory reservation system design and Implementation (graduation thesis + program source code) - Laboratory Reservation System
不用Swagger,那我用啥?
Reptile practice (10): send daily news
201803-3 CCF URL映射 满分题解
[machine learning] naive Bayesian code practice
数据表示与计算(进制)
Leetcode: interview question 08.14. Boolean operation
MySQL事务与MVCC如何实现的隔离级别
Custom configuration
MySQL converts some table names to uppercase
![[machine learning] logistic regression code exercise](/img/dc/203f240e2eb213dbd6173d17e47ec6.jpg)







