当前位置:网站首页>Fundamentals of C language -- 2-5 points of knowledge about pointers and functions
Fundamentals of C language -- 2-5 points of knowledge about pointers and functions
2022-07-23 16:59:00 【Bazinga bingo】
List of articles
Pointer and function pass parameter
Ordinary variables as function parameters
1、 When a function passes parameters , When ordinary variables are used as parameters , Formal and argument names can be the same or different , In fact, it is the argument that replaces the corresponding formal parameter .
2、 Inside the subfunction , The value of the formal parameter is equal to the argument . The reason is that the value of the actual parameter is assigned to the formal parameter when the function is called —— Value transfer call
Array as function parameter
1、 When the array name is passed as a formal parameter , What is actually passed is not the entire array , It's the first address of the first element of the array .( That is, the first address of the entire array ) So inside the subfunction , The array name passed in is equal to a pointer to the first address of the array
2、 The first address of the first element of the array obtained by passing parameters in the sub function , The value of the first address of the first element of the array obtained from the outside is the same .
Pointer as function parameter
It is the same as array as function parameter
Structure variables as function parameters
1、 It is the same as ordinary variables .
2、 Because structures are usually large , So if you directly use structure variables to transfer parameters , Then the efficiency of function call will be very low . Solution : Do not pass variables , Pass the pointer address of the variable into .
Value passing and address calling ( Classic example swap function )
stay C There are no two ways of transmitting value and address in language ,C The language itself always passes values when calling functions , But the value passed can be variable name , It can also be a pointer to a variable .
Input type parameter and output type parameter
1、 The function name is a symbol , Represents the first address of the entire function code segment , In essence, it is a pointer constant , So the function name used in the program is used as the address , Used to call functions .
2、 Function body is the key of function , By a pair { } Cover up , Including many sentences of code , The function body is what the function actually does .
3、 Formal parameter list and return value . The formal parameter is the input part of the function , The return value is the output of the function .
4、 There is no parameter list and return value , Functions can also be used . Parameters can be global variables .
Used in function arguments const The pointer
const Generally used in functions , Usage is const int *p;
const Used to modify pointers to pass parameters to functions , The function is to declare that the content pointed to by this pointer will not be changed inside the function , So pass an immutable pointer to the function (char *p = “linux”) The error will not be triggered , And one that is not declared const Function of the pointer to , When you pass him an unalterable pointer, be careful of mistakes
void func_1(char *p)
{
*p = 'a';
}
void main(void)
{
char *a = "linux"; // There was a paragraph error , as a result of : This string exists in the code segment that cannot be changed
char a[] = "linux"; // Can run , The result is “aiunx”
func_1(a);
return 0;
}
Generally speaking, the input parameters are input parameters , The output parameters are output parameters . Function parameters can have many , There is only one return value . What if the function needs to return many values ?
In programming, the input and output of functions depend on function parameters , The return value is only used to indicate whether the result of function execution is success or failure
If this parameter is used for input , It is called input parameter ; If the purpose of this parameter is output , It is called output parameter .
The output parameter is used to make the data output from the function to the outside
When you see the prototype of a function , How to see which parameters do input and which do output at a glance ?
If the function parameter is a common variable, it must be an input parameter ;
If you pass the pointer, there are two possibilities :
If this parameter is input ( Usually, the input only needs to read this parameter inside the function without changing it ) Just add const To modify
If the function parameter is a pointer variable and has not been added const, Then it means that this parameter is used as an output parameter .
边栏推荐
- Visualization of gross domestic product (GDP) data
- 熵权法优化TOPSIS(MATLAB)
- 解决data functions should return an object 并(Property “visible“ must be accessed with “$data.visible“)
- 软件测试计划包括哪些内容,测试计划如何编写。分享测试计划模板
- 零基础怎么自学软件测试?十年测试老鸟最强软件测试学习路线图
- RISC-V基金会董事谭章熹:RISC-V,从边缘逐渐向中央扩展
- Notes on Microcomputer Principle and technical interface
- It's not safe to open an account at qiniu business school
- CNCF基金会总经理Priyanka Sharma:一文读懂CNCF运作机制
- 深度学习卷积神经网络论文研读-AlexNet
猜你喜欢

【31. 走迷宫(BFS)】

灰色预测(MATLAB)

The first stage of basic knowledge of numpy data analysis (numpy Foundation)

小米集团副总裁崔宝秋:开源是人类技术进步的最佳平台和模式

距离IoU损失:包围盒回归更快更好的学习(Distance-IoU Loss: Faster and Better Learning for Bounding Box Regression)

Cuibaoqiu, vice president of Xiaomi group: open source is the best platform and model for human technological progress

COPU副主席刘澎:中国开源在局部领域已接近或达到世界先进水平

RISC-V基金会董事谭章熹:RISC-V,从边缘逐渐向中央扩展

微机原理与技术接口笔记

Nodejs implements token login registration (koa2)
随机推荐
网络协议与攻击模拟:wireshark使用、ARP协议
Four cores of browser
Lake Shore—EMPX-H2 型低温探针台
General paging function
leetcode-67.二进制求和
TOPSIS法(MATLAB)
低代码搭建校园信息化管理系统案例分析
Direct exchange
主成分分析(MATLAB)
距离IoU损失:包围盒回归更快更好的学习(Distance-IoU Loss: Faster and Better Learning for Bounding Box Regression)
【31. 走迷宫(BFS)】
通用分页功能
Nifi 1.16.3 cluster setup +kerberos+ user authentication
C语言基础篇 —— 2-5 指针与函数知识点
How does MySQL query data that is not in the database?
Bag of tricks for image classification "with convolutional neural networks"
SSD: Single Shot MultiBox Detector
Distance IOU loss: faster and better learning for bounding box regression
Leetcode-67. binary sum
tensorflow一层神经网络训练手写体数字识别