当前位置:网站首页>【C语言】数组知识点总结
【C语言】数组知识点总结
2022-07-17 10:21:00 【安苒_】
数组:一组 相同类型元素的集合,这些数据在内存中 连续存放。
一、一维数组
创建和初始化
//创建
type_t arr_name[const_n];
//type_t 是指数组的元素类型
//const_n 是一个常量表达式,用来指定数组的大小
//注:这里const_n的位置我们习惯用常量/宏常量
//原因:C99标准之前,[] 中要给一个常量才可以,不能使用变量。在C99标准支持了变长数组的概念。
//初始化——不完全初始化(自动填充——0,'\0'等)||完全初始化
//不完全
int arr[10] = {
1,2,3,4,5 };
//完全
int arr2[10] = {
1,2,3,4,5,6,7,8,9,10};
//省略数组长度的初始化
int arr4[] = {
1,2,3,4};
使用
//元素的访问
arr[0]=5;
//注:数组是使用下标来访问的,下标是从0开始
//数组长度的计算
int arr[] = {
1,2,3,4,5,6,7,8,9,10 };
int sz = sizeof(arr) / sizeof(arr[0]);
在内存中的存储
数组在内存中是连续存放的
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BvxHaWip-1657964008901)(C:\Users\19271\AppData\Roaming\Typora\typora-user-images\image-20220715022719515.png)]](/img/13/9897cd16899ecac48760e4dc316e3e.png)
二、多维数组
以二维数组居多,所以这里主要讨论二维,再多维其实是相似的。
创建和初始化
//创建示例
int arr1[3][3];
//初始化
//不完全初始化——从头排列、分块
int arr[3][4] = {
1,2,3,4,5 }; //从头
int arr[3][4] = {
{
1,2 },{
3,4 },{
5 } }; //分行
//省略行初始化
int arr[][4] = {
{
1,2 },{
3,4 },{
5 } };
int arr[][4] = {
1,2,3,4,5,6,7,8};
//完全初始化——略
使用
//打印
int main()
{
int arr[][3] = {
{
1,2 },{
3,4 },{
5 } };
int i = 0; //行
int j = 0; //列
for (i = 0; i < 3; i++) //控制行
{
for (j = 0; j < 3; j++) //控制列
{
printf("%d ", arr[i][j]);
}
printf("\n");
}
return 0;
}
在内存中的存储
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-NfNb0YN4-1657964008904)(C:\Users\19271\AppData\Roaming\Typora\typora-user-images\image-20220715023304489.png)]](/img/82/f3b76650fe8390f8929b2d3134b01d.png)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-uoIGrCEu-1657964008905)(C:\Users\19271\AppData\Roaming\Typora\typora-user-images\image-20220715023320141.png)]](/img/7a/2dc17c2f27d03ecab89aac30b3fa61.png)
二维数组每个元素的地址都是连续的 ,二维数组在内存中也是连续存放的.
三、常见问题
数组越界
数组的下标范围:0~n-1
C语言本身是不做数组下标的越界检查,编译器也不一定报错,但是编译器不报错,并不意味着程序就是正确的,所以程序员写代码时,最好自己做越界的检查
数组作为函数参数
由于数组名本质是数组首元素地址,所以接收参数的时候可以有两种
①指针形式
②数组形式—注意多维时只有第一个数字可以省略
边栏推荐
- SSM implementation of one-to-one query detailed tutorial (1)
- 岚图梦想家的产品力到底如何?
- Development utility
- Programming in the novel [serial 11] the moon bends in the yuan universe
- LDA classifier
- codeforces每日5题(均1500)-第十七天
- 【愚公系列】2022年7月 Go教学课程 012-强制类型转换
- AnyControl Demo演示
- [Network Research Institute] the threat of machine learning system is time to take it seriously
- 两个结构体 ifconf 和 ifreq
猜你喜欢

静态路由!!静态路由!!静态路由!!

Uniapp warehouse management system source code
![[hero planet July training leetcode problem solving daily] 17th kuansou](/img/92/9b8e3a710430d37564d7ea3f28168f.png)
[hero planet July training leetcode problem solving daily] 17th kuansou

【网络研究院】机器学习系统的威胁是时候该认真对待了

多租户 SaaS 的数据库设计模式,你学废了吗?

Towhee daily model weekly report
![[performance optimization methodology series] VI. summary](/img/46/81261e4ed5c1efe48acb979ac39f7b.jpg)
[performance optimization methodology series] VI. summary

2、 Pinda general permission system__ Project construction

Markdown (5): anchor link

Static routing!! Static routing!! Static routing!!
随机推荐
C语言编译过程
MySQL升级为主备,如何同步历史数据?
es概念模型与基本故障
How long does software testing take?
Express
Project contract analysis from the perspective of Software Engineer
MySQL view
Tree array
Yanrong technology was selected as Beijing's "specialized and innovative" in 2022 to lead hybrid cloud file storage
OLED显示如何理解 12*6、16*8、24*12等字符大小
C语言基础篇 —— 2-2 const关键字与指针
企业数字化转型,为何 SaaS 模式如此重要?
Fundamentals of C language -- 2-1 pointer and wild pointer
Day 7 Training
ETCD数据库源码分析——etcdserver bootstrap从快照中恢复store
Towhee daily model weekly report
Utility series - xshell installation, download and use
力扣(LeetCode)197. 上升的温度(2022.07.16)
v-mode
Programming in the novel [serial 15] the moon bends in the yuan universe