当前位置:网站首页>数组相关知识
数组相关知识
2022-07-27 22:10:00 【徐憨憨!】
继学完函数相关知识之后,我们接下来学习数组的相关知识,我们都知道当我们定义一个整型变量时,会定义int a = 0,再定义一个整型变量时会定义int b = 0....当需要多个整型变量时,就会定义int类型的变量,但是同样的操作重复多次会显得代码十分冗杂,所以这里就引入了数组的定义,把多个相同类型的变量组合在一起便是数组!!!
目录:
1. 一维数组的创建和初始化2. 一维数组的使用3. 一维数组在内存中的存储4. 二维数组的创建和初始化5. 二维数组的使用6. 二维数组在内存中的存储7. 数组越界8. 数组作为函数参数9. 数组的应用实例1:三子棋10. 数组的应用实例2:扫雷游戏
一· 一维数组
1.1 一维数组的创建
数组的创建方式:
type_t arr_name [const_n];//type_t 是指数组的元素类型//const_n 是一个常量表达式,用来指定数组的大小
示例:
int arr[10] = { 0 };//定义一个10个整型元素的数组,数组名为arr,数组内元素的类型时int类型
1.2 一维数组的初始化
数组的初始化分为完全初始化和不完全初始化,不完全初始化的时候,数组会根据数组的大小将剩下的元素默认为0,数组在创建的时候如果想不指定数组的确定的大小就得初始化,数组的元素个数根据初始化的内容来确定。
完全初始化:
int arr[4] = {1,2,3,4}//数组中有4个元素,分别为1 2 3 4
不完全初始化:
int arr[4] = {1,2}//数组中有4个元素,分别为1 2 0 0
未指定数组大小
int arr[] = {1,2,3,4}//数组根据初始化内容默认数组中有4个元素,分别为1 2 3 4
未指定数组大小且不初始化
int arr[] = {0}//默认数组中只有一个元素为0
字符型数组:
我们来看一段代码来思考一下其编译结果 ,从而加深对strlen的理解
#include <string.h>
int main()
{
char arr1[] = "abcd";
char arr2[] = { 'a','b','c','d' };
char arr3[4] = { 'a','b','c','d' };
char arr4[5] = { 'a','b','c','d' };
printf("%d\n", strlen(arr1));
printf("%d\n", strlen(arr2));
printf("%d\n", strlen(arr3));
printf("%d\n", strlen(arr4));
return 0;
}

1.3 一维数组的使用
分为输入和打印
int main()
{
int arr[10] = 0;
int i = 0;
//输入
for (i = 0; i < 10; i++)
{
scanf("%d", &arr[i]);
}
//打印
for (i = 0; i < 10; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
return 0;
} 
总结:
1.4 一维数组在内存中的存储
二· 二维数组
2.1 二维数组的创建
int arr[2][3] = {0}//创建了一个两行三列,6个整型元素的数组
2.2 二维数组的初始化
同一维数组初始化一样,分为完全初始化和不完全初始化
int arr[3][4] = { 1,2,3,4};int arr[3][4] = { { 1,2},{ 4,5}};int arr[][4] = { { 2,3},{ 4,5}};
注意:二维数组如果有初始化,行可以省略,列不能省略
2.3 二维数组的使用
int main()
{
int arr[3][4] = { 0 };
int i = 0;
//输入
for (i = 0; i < 3; i++)
{
int j = 0;
for (j = 0;j < 4; j++)
{
scanf("%d", &arr[i][j]);
}
}
//打印
for (i = 0; i < 3; i++)
{
int j = 0;
for (j = 0;j < 4; j++)
{
printf("%d ", arr[i][j]);
}
printf("\n");
}
printf("\n");
return 0;
}
2.4 二维数组在内存中的存储
同一维数组一样,递增
三· 数组越界
一个数组有n个元素时,数组下标从0开始,可以访问到n-1,当数组下标访问的时候小于0或者大于n-1时,机会越界访问。
四· 数组作为函数参数
数组作为函数参数时是如何传参与接收的,我将用一个冒泡排序算法给大家进行讲解
冒泡排序算法思想:
假如有n个元素我们需要从小到大将其排序时,n个元素我们需要排序n-1次,因为最后一个元素会自动排好序,每进行一次排序的时候,需要从第一个元素开始一次比较相邻两个元素的大小,不满足排序规则则需要进行交换,第一次排序需要交换n-1对元素,第二次交换需要交换n-2对元素......第j次排序则需要交换n-j对元素,所以在排序的函数内部需要嵌套两个循环。
void Bubble_sort(int arr[], int sz)
{
//排序的趟数
int i = 0;
for (i = 0; i < sz - 1; i++)
{
//比较的对数
int j = 0;
for (j = 0; j < sz - 1 - i; j++)
{
if (arr[j] > arr[j + 1])
{
int tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
}
}
int main()
{
int arr[] = { 5,6,7,1,9,4,8,3,0,12 };
int sz = sizeof(arr) / sizeof(arr[0]);
Bubble_sort(arr, sz);
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
printf("\n");
return 0;
} 
注意:
在上述代码中,数组元素的个数sz我们是把它当成参数传入函数中的,有的人会问为什么不在函数内部进行计算,这样在函数传参的时候不就会减少一个变量了嘛? 其实这个原因在函数那个博客中我们也说明过。
因为数组传参时,传递的是首元素的地址,虽然接受的是int arr[],但是实际上它是一个指针变量,并非一个整型数组,既然它是一个指针变量,那么在函数内部计算sz的时候,sizeof(arr),sizeof(arr[0])均表示首元素地址,sizeof(arr)/sizeof(arr[0]) = 1,这与我们定义的数组有10个变量就会矛盾。
那么数组名究竟是什么?我们来深入研究一下!
我们只需记住:
//数组名表示数组首元素的地址,两种情况除外
//1.sizeof(数组名),表示整个数组所占用的空间大小,单位为字节
//2.&数组名,取出的是整个数组的地址,结果是数组第一个元素的地址,但是&数组名+1就会跳过整个数组的地址
int main()
{
int arr[10] = { 0 };
printf("%p\n", arr);
printf("%p\n", arr + 1);
printf("%p\n", &arr[0]);
printf("%p\n", &arr[0] + 1);
printf("%p\n", &arr);
printf("%p\n", &arr+1);
return 0;
}

arr &arr[0] &arr结果相同,但是&arr表示的是整个数组的地址,&arr+1就会跳过整个数组的大小(这里是40个字节),故两次地址相差40,但是这里是16进制表示的;其余两个arr+1和&arr[0]+1都只会跳过一个数据,故两次地址相差一个int类型的字节:4。
数组的知识全部结束啦,希望大家支持,数组相关的应用是两个游戏,我将会另写博客进行讲解!!!
边栏推荐
- Matlab | those matlab tips you have to know (I)
- FFT 采样频率和采样点数的选取
- 网络设备硬核技术内幕 防火墙与安全网关篇 (八) 虚拟化神器 (中)
- OpenVINO整合TensorFlow实现推理加速
- The influence of head zeroing and tail zeroing on FFT output
- Focus on demand flow rather than idle people
- y79.第四章 Prometheus大厂监控体系及实战 -- prometheus的服务发现机制(十)
- A few lines of code can easily realize the real-time reasoning of paddleocr. Come and get!
- Code review tool
- startUMl
猜你喜欢

Basic operations of MySQL database (3) --- Based on fields
![[proteus simulation] 51 single chip microcomputer washing machine simulation control program](/img/a1/d4256a5e350078b791c0b5cd512fe3.png)
[proteus simulation] 51 single chip microcomputer washing machine simulation control program

Intel joins hands with hanshuo and Microsoft to release the "Ai + retail" trick!

Leetcode 415. string addition and 43. string multiplication
![[leetcode] 547. Number of provinces (medium)](/img/15/d49d18151c47e318fe7acabdd616e6.png)
[leetcode] 547. Number of provinces (medium)

OpenVINO整合TensorFlow实现推理加速

【Meetup预告】OpenMLDB+OneFlow:链接特征工程到模型训练,加速机器学习模型开发

阿里二面:为什么要分库分表?

激活最大化

Ali Er Mian: why do we need to separate databases and tables?
随机推荐
threejs个人笔记
In the first quarter of 2020, the wearable market shipped 72.6 million units, with apple occupying nearly 30% of the market share
Basic operations of MySQL database (2) --- Based on data table
递归求解迷宫问题
Invest 8billion! Nanjing Huatian sealed test phase I project is about to be put into production!
The influence of head zeroing and tail zeroing on FFT output
Network device hard core technology insider firewall and security gateway (VIII) virtualization artifact (middle)
Jerry Zhi doesn't play hidden audio files [article]
7. Typescript part Foundation
LSB steganography
How does matlab set the K-line diagram to classic red and green color matching?
The server is poisoned - the dish is the original sin
Possible reasons why there is no voltage in the corresponding channel, but the ADC value is changing greatly and is not equal to 0
Redis transaction and optimistic lock
Fastjson历史漏洞复现
Network equipment hard core technology insider firewall and security gateway chapter (VI) security double repair under the law
FFT 采样频率和采样点数的选取
y79.第四章 Prometheus大厂监控体系及实战 -- prometheus的服务发现机制(十)
估值360亿美元!即将进行首次载人发射的SpaceX筹资3.46亿美元
自动推理的逻辑09–自动定理证明