当前位置:网站首页>AUTOSAR从入门到精通番外篇(八)-C语言常用技巧50个
AUTOSAR从入门到精通番外篇(八)-C语言常用技巧50个
2022-08-11 08:00:00 【格图洛书】
01. 宏定义用do{}while(0)
如果定义的宏函数后面有多条语句,使用这样的方式会有问题:
#define FUNC() func1(); func2() if(bRunF) FUNC();
展开宏定义后会变成:
if(bRunF) func1(); func2();
逻辑就不对了。可以用这一的方式解决,非常好用:
#define FUNC() do{func1(); func2();}while(0)
02. 数组的初始化
假如给arr的第2~6元素初始化为5,也许你会
int arr[10] = {0, 5, 5, 5, 5, 5, 0, 0, 0, 0};
现在告诉你C99可以这样:
int arr[10] = {[1... 5] = 5};
03. 数组的访问
你想取数组的第6个元素(下标为5),教科书教你这样做:
int arr[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int n1 = arr[5]; int n2 = *(arr+5);
其实你可以:
int arr[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int n = 5[arr];
也不会有错,实际上arr[5]对应*(arr+5)
,而5[arr]对应*(5+arr)
,没多大区别。
04. 结构体的初始化
结构体的初始化,传统的
边栏推荐
- My creative anniversary丨Thank you for being with you for these 365 days, not forgetting the original intention, and each is wonderful
- 零基础SQL教程: 基础查询 05
- Machine Learning Summary (2)
- 链式编程注解
- 高德能力API
- 初级软件测试工程师笔试试题,你知道答案吗?
- string类接口介绍及应用
- 1081 Check Password (15 points)
- Interaction of Pico neo3 in Unity
- 1056 组合数的和 (15 分)
猜你喜欢
进阶-指针
1071 Small Gamble (15 points)
2.1 - Gradient Descent
查找最新人员工资和上上次人员工资的变动情况
Redis source code: how to view the Redis source code, the order of viewing the Redis source code, the sequence of the source code from the external data structure of Redis to the internal data structu
JRS303-数据校验
Four operations in TF
TF generates (feature, label) set through feature and label, tf.data.Dataset.from_tensor_slices
1003 我要通过 (20 分)
初级软件测试工程师笔试试题,你知道答案吗?
随机推荐
pyqt5实现仪表盘
少年成就黑客,需要这些技能
1003 我要通过 (20 分)
Redis source code: how to view the Redis source code, the order of viewing the Redis source code, the sequence of the source code from the external data structure of Redis to the internal data structu
1106 2019 Sequence (15 points)
FPGA 20个例程篇:11.USB2.0接收并回复CRC16位校验
2022-08-10 mysql/stonedb-慢SQL-Q16-耗时追踪
1091 N-Defensive Number (15 points)
机器学习(三)多项式回归
C Primer Plus(6) 中文版 第1章 初识C语言 1.7 使用C语言的7个步骤
Write a resume like this, easy to get the interviewer
C语言-结构体
关于架构的认知
如何仅更改 QGroupBox 标题的字体?
2022 China Soft Drink Market Insights
1036 Programming with Obama (15 points)
Creo9.0 特征的成组
优炫数据库支持多列分区吗?
Break pad source code compilation--refer to the summary of the big blogger
剑指offer专项突击版第26天