当前位置:网站首页>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
边栏推荐
- The use and Simulation of string function, character function and memory function
- Appendix 2 – some simple exercises
- 核酸扫码登记体验有感(如何提高OCR的文字正确识别率)
- Flowable UI production flow chart
- No swagger, what do I use?
- Flutter文本编辑器
- Quick sorting (quick sorting) (implemented in C language)
- Unity3d learning notes (I)
- Configuration file settings for remote connection to Windows version server redis
- MySQL converts some table names to uppercase
猜你喜欢
随机推荐
"Defects" of prototype chain inheritance and constructor inheritance
数仓项目踩坑记录与解决方法总结
简述堆和栈的区别
I don't know how lucky the boy who randomly typed logs is. There must be a lot of overtime
On contract testing
A structured random inactivation UNET for retinal vascular segmentation
Want to know how to open an account through online stock? Excuse me, is it safe to open a stock account by mobile phone?
高智伟:数据管理赋能交通行业数字化转型
基于ArkUI eTS开发的坚果新闻(NutNews)
One click automated data analysis! Come and have a look at these treasure tool libraries
用户身份标识与账号体系实践
云原生管理实践:业务引领的DevOps持续交付体系
核酸扫码登记体验有感(如何提高OCR的文字正确识别率)
Using logistic regression and neural network to deal with complex binary classification problems
Retinal Vessel Segmentation via a Semantics and Multi-Scale Aggregation Network
23 postgraduate entrance examination people hold on! The first wave of exam abandonment peak has arrived!
Sublime text create page
Custom configuration
How to realize the isolation level between MySQL transactions and mvcc
MySQL error summary