当前位置:网站首页>Introduction of several ways to initialize two-dimensional arrays in C language (private way to initialize large arrays)
Introduction of several ways to initialize two-dimensional arrays in C language (private way to initialize large arrays)
2022-08-11 05:45:00 【FussyCat】
CSeveral ways of initializing two-dimensional arrays in the language are introduced
1、直接赋值
Suitable for arrays with fewer elements,The value of each element can be different.
int arr1[2][3] = {
{
5, 2, 4}, {
10, 2, 1} };
int arr1[2][3] = {
0}; /* 所有元素都初始化为0 */
int arr1[2][3] = {
1}; /* 只有arr1[0][0]为1,All other elements are initialized to 0 */
2、The loop assigns values to each element
Assign a value to each element of the array,The value of each element can be different.就是效率比较低.
int arr2[2][3];
int i, j;
for (i = 0; i < 2; i++) {
for (j = 0; j < 3; j++) {
arr2[i][j] = 2; /* In this example, both are assigned the same value for simplicity */
}
}
3、借用memset/memset_s初始化为0或-1
注意:memset/memset_sVariables can only be initialized as 0或-1
,Other values do not hold. 参考百度百科 The first of the common mistakes.
A lot of people don't notice this,容易犯错.
int arr3[10][10];
memset(arr3, 0, sizeof(arr3); /* 正常,arr3中的每个元素都为0 */
memset(arr3, -1, sizeof(arr3); /* 正常,arr3中的每个元素都为-1 */
memset(arr3, 2, sizeof(arr3); /* 异常,arr3Each element in is an outlier33686018 */
4、All elements of the array are initialized to the same value(It is convenient for initialization of large arrays)
as long as the values are the same,Especially when there are many array elements,推荐用此方法:
{ [0 … LENA-1][0 … LENB-1] = num };
This initialization method is relatively rare,But especially convenient,所以共享给大家.
#define ARR_LEN 100
int arr4[ARR_LEN][ARR_LEN] = {
[0 ... (ARR_LEN-1)][0 ... (ARR_LEN-1)] = 10 }; /* 100*100个元素都初始化为10 */
边栏推荐
- (2) Construction of a real-time performance monitoring platform (Grafana+Prometheus+Jmeter)
- 【翻译】博客游戏项目Q1K3 – 制作
- curl 命令调用接口demo
- LeetCode43.字符串相乘 (大数相乘可用此方法)
- (三)性能实时监控平台搭建(Grafana+Prometheus+Node_explorer+Jmeter)
- 面试宝典一: code题目记录
- task02 fashion-mnist分类实战
- flaks framework learning: adding variables to the URL
- [Verilog] I2S Master Test Bench
- Flask框架学习:模板继承
猜你喜欢
看完这篇博客之后,含着泪学会了TCP/IP
Flask框架学习:模板继承
第二篇 DS5 Armv8 样例工程报错之GCC编译
(2) Construction of a real-time performance monitoring platform (Grafana+Prometheus+Jmeter)
QT GrabWindow截取屏幕
(一)Docker安装Redis实战(一主二从三哨兵)
(二)Docker安装Redis实战(持久化AOF和RDB快照)
搭建PX4开发环境
(三)性能实时监控平台搭建(Grafana+Prometheus+Node_explorer+Jmeter)
【win10+cuda7.5+cudnn6.0安装caffe⑥】报错及处理方式
随机推荐
Solidrun hummingboard制作SD卡
pytorch中tensor 生成的函数
[C language from elementary to advanced] Part 2 Initial C language (2)
面试宝典二:nlp常见知识点
普林斯顿概率论读本读书笔记(阅读中......)
(二)Docker安装Redis实战(持久化AOF和RDB快照)
Blender 初教程
arraylist之与linkedlist
c 指针学习(1)
LeetCode43. String multiplication (this method can be used to multiply large numbers)
lspci 命令使用
(一)Docker安装Redis实战(一主二从三哨兵)
一、Jmeter环境部署
【C语言从初阶到进阶】第二篇 初始C语言(二)
LeetCode1166.设计文件系统
CSDN 社区内容创作规范
【背包】采药题解
吃瓜教程task02 第3章 线性模型
怎么用管理员方式打开压缩包
pip 国内源下载