当前位置:网站首页>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
边栏推荐
- Quick sorting (quick sorting) (implemented in C language)
- 网络原理笔记(五层网络)
- Summary of pit trampling records and solutions of data warehouse project
- No swagger, what do I use?
- ERROR 1045 (28000): Access denied for user ‘ODBC‘@‘localhost‘ (using password: NO)
- Want to know how to open an account through online stock? Excuse me, is it safe to open a stock account by mobile phone?
- First order traversal / second order traversal determines the approximate shape of the tree
- 1.2.24 fastjson deserialization templatesimpl uses chain analysis (very detailed)
- Commonly used DOS commands [gradually improved]
- Unity guidance system. Click the target object and prompt the text to change color to enter the next step
猜你喜欢

Database system design: partition

Asp graduation project - based on C # +asp Design and implementation of enterprise investment value analysis system based on. Net + sqlserver (graduation thesis + program source code) -- enterprise in

STM32 application development practice tutorial: design and implementation of controllable LED water lamp

远程连接windows版本服务器redis的配置文件设置

Cloud native management practice: business led Devops continuous delivery system

Unity xchart3.0 basic usage quick start

Flowable UI制作流程图

WebAssembly 2022 问卷调查结果新鲜出炉

【机器学习】朴素贝叶斯代码练习

Floweable advanced
随机推荐
乱打日志的男孩运气怎么样我不知道,加班肯定很多
如何介绍自己的项目经验?
First order traversal / second order traversal determines the approximate shape of the tree
How to realize the isolation level between MySQL transactions and mvcc
Summary of research on endogenous information security technology of industrial measurement and control equipment
数仓项目踩坑记录与解决方法总结
How to change MySQL into Chinese
Redis series 3: highly available master-slave architecture
NFA determination and DFA minimization based on C language
201803-3 Full Score solution of CCF URL mapping
原型链继承和构造函数继承的 “毛病”
dataframe. to_ Sql() inserts too many errors at one time
Flowable 基础篇1
存算一体与存内计算计算杂谈
OpenCV图像处理基础操作
云原生管理实践:业务引领的DevOps持续交付体系
Safety is no longer the only selling point. Test drive "slash youth" Volvo C40
数据表示与计算(进制)
Flowable UI制作流程图
Summary of some experiences in the process of R & D platform splitting