当前位置:网站首页>c语言—数组
c语言—数组
2022-07-06 17:30:00 【小突突~】
目录
1. 一维数组的创建和初始化
1.1 数组的创建
数组是一组相同类型元素的集合
数组的创建方式:
type_t arr_name [const_n];
//type_t 是指数组的元素类型
//const_n 是一个常量表达式,用来指定数组的大小
数组创建的实例:
注:C99中引入了变长数组的概念,允许数组的大小用变量来指定,如果编译器不支持C99中的变长数组,那就不能使用。
1.2 数组的初始化
数组的初始化是指,在创建数组的同时给数组的内容一些合理初始值(初始化)。
int arr1[10] = {1,2,3};
int arr2[] = {1,2,3,4};
int arr3[5] = {1,2,3,4,5};
char arr4[3] = {'a',98, 'c'};
char arr5[] = {'a','b','c'};
char arr6[] = "abcdef";
数组在创建的时候如果想不指定数组的确定的大小就得初始化。数组的元素个数根据初始化的内容来确定。
但是对于下面的代码要区分,内存中如何分配。
char arr1[] = "abc";
char arr2[3] = {'a','b','c'};
1.3 一维数组的使用
对于数组的使用我们之前介绍了一个操作符: [ ] ,下标引用操作符。它其实就数组访问的操作符
#include <stdio.h>
int main()
{
int arr[10] = {0};//数组的不完全初始化
//计算数组的元素个数
int sz = sizeof(arr)/sizeof(arr[0]);
//对数组内容赋值,数组是使用下标来访问的,下标从0开始。所以:
int i = 0;//做下标
for(i=0; i<10; i++)//这里写10,好不好?
{
arr[i] = i;
}
//输出数组的内容
for(i=0; i<10; ++i)
{
printf("%d ", arr[i]);
}
return 0;
}
数组是使用下标来访问的,下标是从0开始。数组的大小可以通过计算得到。
1.4 一维数组在内存中的存储
int main
{
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
int sz = sizeof(arr) / sizeof(arr[0]);
int i = 0;
int* p = &arr[0];
for (i = 0; i < sz; i++)
{
printf("%d ", *(p + i));
}
printf("\n");
for (i = 0; i < sz; i++)
{
printf("&arr[%d] = %p <=======> %p\n", i, &arr[i], p + i);
}
return 0;
}
仔细观察输出的结果,我们知道,随着数组下标的增长,元素的地址,也在有规律的递增。
由此可以得出结论:数组在内存中是连续存放的。
2. 二维数组的创建和初始化
2.1 二维数组的创建
//数组创建
int arr[3][4];
char arr[3][5];
double arr[2][4];
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 二维数组的使用
二维数组的使用也是通过下标的方式
#include <stdio.h>
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++)
{
arr[i][j] = i*4+j;}
}
for(i=0; i<3; i++)
{
int j = 0;
for(j=0; j<4; j++)
{
printf("%d ", arr[i][j]);
}
}
return 0;
}
2.4 二维数组在内存中的存储
像一维数组一样,这里我们尝试打印二维数组的每个元素。
int main()
{
int arr[][5] = { {1,2},{3,4},{5,6} };
int i = 0;
for (i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
{
int j = 0;
for (j = 0; j < sizeof(arr[0]) / sizeof(arr[0][0]); j++)
{
printf("&arr[%d][%d] = %p\n",i,j ,&arr[i][j]);
}
}
return 0;
}
通过结果我们可以分析到,其实二维数组在内存中也是连续存储的
3. 数组越界
数组的下标是有范围限制的。
数组的下规定是从0开始的,如果数组有n个元素,最后一个元素的下标就是n-1。
所以数组的下标如果小于0,或者大于n-1,就是数组越界访问了,超出了数组合法空间的访问。
C语言本身是不做数组下标的越界检查,编译器也不一定报错,但是编译器不报错,并不意味着程序就是正确的,
所以程序员写代码时,最好自己做越界的检查。
#include <stdio.h>
int main()
{
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
int i = 0;
for(i=0; i<=10; i++)
{
printf("%d\n", arr[i]);//当i等于10的时候,越界访问了
}
return 0;
}
二维数组的行和列也可能存在越界。
4. 数组作为函数参数
往往我们在写代码的时候,会将数组作为参数传个函数 ,比如:我要实现一个冒泡排序(这里要讲算法思想)函数将一个整形数组排序。
4.1 冒泡排序函数的错误设计
#include <stdio.h>
void bubble_sort(int arr[])
{
int sz = sizeof(arr)/sizeof(arr[0]);//这样对吗?
int i = 0;
for(i=0; i<sz-1; i++)
{
int j = 0;
for(j=0; j<sz-i-1; j++)
{
if(arr[j] > arr[j+1])
{
int tmp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tmp;
}
}
}
}
int main()
{
int arr[] = {3,1,7,5,8,9,0,2,4,6};
bubble_sort(arr);//是否可以正常排序?
for(i=0; i<sizeof(arr)/sizeof(arr[0]); i++)
{
printf("%d ", arr[i]);
}
return 0;
}
这个方法出问题了,那我们找一下问题,调试之后可以看到 bubble_sort 函数内部的 sz ,是1。
难道数组作为函数参数的时候,不是把整个数组的传递过去?
4.2 数组名是什么?
有2个例外:
1.sizeof(数组名),数组名不是数组首元素的地址,数组名表示整个数组,计算的是整个数组的大小.
2. &数组名,数组名不是数组首元素的地址,数组名表示整个数组,取出的是整个数组的地址
除此1,2两种情况之外,所有的数组名都表示数组首元素的地址。
4.3 冒泡排序函数的正确设计
#include <stdio.h>
//实质上是一个指针 void bubble_sort(int* arr)
void bubble_sort(int arr[],int sz)
{
//int sz = sizeof(arr) / sizeof(arr[0]);//里面算不出来sz,因为只传了一个指针。
int i = 0;
//趟数
for (i = 0; i < sz; i++)
{
int flag = 1;//假设条件是升序
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;
flag = 0;
}
if (1 == flag)
{
break;
}
}
}
}
int main()
{
int arr[] = {10,9,8,7,6,5,4,3,2,1};
int sz = sizeof(arr) / sizeof(arr[0]);
bubble_sort(arr,sz);
//int sz = sizeof(arr) / sizeof(arr[0]);
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
边栏推荐
- pyflink的安装和测试
- 「精致店主理人」青年创业孵化营·首期顺德场圆满结束!
- Pytorch中torch和torchvision的安装
- Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
- 做微服务研发工程师的一年来的总结
- tensorflow 1.14指定gpu运行设置
- [batch dos-cmd command - summary and summary] - string search, search, and filter commands (find, findstr), and the difference and discrimination between find and findstr
- 【js】获取当前时间的前后n天或前后n个月(时分秒年月日都可)
- The printf function is realized through the serial port, and the serial port data reception is realized by interrupt
- HMM notes
猜你喜欢
[100 cases of JVM tuning practice] 05 - Method area tuning practice (Part 2)
HMM notes
Come on, don't spread it out. Fashion cloud secretly takes you to collect "cloud" wool, and then secretly builds a personal website to be the king of scrolls, hehe
迈动互联中标北京人寿保险,助推客户提升品牌价值
ESP Arduino (IV) PWM waveform control output
Windows installation mysql8 (5 minutes)
Data type of pytorch tensor
windows安装mysql8(5分钟)
重上吹麻滩——段芝堂创始人翟立冬游记
系统休眠文件可以删除吗 系统休眠文件怎么删除
随机推荐
Informatics Olympiad YBT 1171: factors of large integers | 1.6 13: factors of large integers
[Niuke] [noip2015] jumping stone
Explain in detail the matrix normalization function normalize() of OpenCV [norm or value range of the scoped matrix (normalization)], and attach norm_ Example code in the case of minmax
自旋与sleep的区别
Data type of pytorch tensor
HMM notes
Build your own website (17)
windows安装mysql8(5分钟)
Can the system hibernation file be deleted? How to delete the system hibernation file
Boot - Prometheus push gateway use
boot - prometheus-push gateway 使用
ZABBIX 5.0: automatically monitor Alibaba cloud RDS through LLD
资产安全问题或制约加密行业发展 风控+合规成为平台破局关键
负载均衡性能参数如何测评?
JTAG debugging experience of arm bare board debugging
[牛客] B-完全平方数
The difference between spin and sleep
Implementation principle of waitgroup in golang
Dell笔记本周期性闪屏故障
NEON优化:关于交叉存取与反向交叉存取