当前位置:网站首页>[C language] Explicit array solution (1)
[C language] Explicit array solution (1)
2022-08-02 13:22:00 【mortal programming biography】
作者:凡人编程传
系列:C语言初阶(适合小白入门)
说明:以凡人之笔墨,书写未来之大梦
文章目录
*前言
哈喽!几日不见,甚是想念啊,I went to the hospital because of some physical problems two days ago.,Okay now, come back and keep rolling!好了,We are entering a new chapter——数组.The content of this chapter is also very important,This array is very similar to the sequence table of the following data structure, so it is worth learning carefully,把基础打好,In order to deal with the big data structure and algorithm behindboss.好了,咱们直接进入正题!
*一维数组
You must have seen this thing many times in the code of the previous article..His full name is one-dimensional array.Is what is called a one-dimensional array?You must have heard of dimensions..For example, the space we live in now is a three-dimensional space,One-dimensional fingers are only long,growing hereCThe language refers to the length of the array(行),that one-dimensional array,Is there a two-dimensional array??答案是有的.Two-dimensional arrays add the concept of a column.But don't be too imaginative,There are no three-dimensional arrays,如果有,So that three-dimensional don't you jump out of the compiler hit you?
一维数组的创建和初始化
- One-dimensional arrays are created as follows:
int arr[3];
The meaning of this code is created, called an arrayarr,并且有3个类型为int的元素.
- 一维数组的初始化
(1)完全初始化(Initialization means that it is assigned by the way when it is created.)
int arr[3]={1,2,3};
(2)不完全初始化
int arr[5]={1,2,3};
这两个有啥区别啊?Let's debug the code and take a look
可以看到,Only three of the elements in the first,And all are assigned numbers according to initialization.第二种有5个元素,只有前3elements initialized with the specified number,The next two elements default to0.
由上可知:
1.One-dimensional arrays will be下标(数组的下标是从0开始),used to identify elements.如arr[1]对应的就是数字2)Store data from front to back.
2.When a one-dimensional array is not fully initialized,Elements that are not initialized later will be defaulted to0,(当数组类型是char的时候默认的是\0)
一维数组的使用
1.Used to continuously enter a set of data
有的人就说了,为啥不用scanf?then let you enter1-10000数字,do you enter one by one?累不累啊?Use arrays with loops in minutes.
#include<stdio.h>
int main()
{
int arr[1000];
int i=0;
for(i=0;i<1000;i++) //因为数组下标是从0开始,所以i从0开始初始化
{
arr[i]=i;
}
return 0;
}
Also remind you some here,When creating an array it cannot be written asint arr[n];的形式,[]There must be a integer constant,当然在C99The standard adds the concept of a flexible array,如果你的编译器支持C99The standard can write boldly like this.
2.配合sizeofoperator to count the number of arrays
sizeofoperator used to calculate the size of a data,单位是字节(byte).那么我们知道Int的长度是4个字节,那么int arr[3];数组里面有3个int类型的元素,那么1个inttype of element stamp4个字节,所以3个IntThe length of the type is3 * 4=12个字节,That is, the length of the entire array is12个字节.那么一个int类型是4个字节.to get a formula ——total length of elements/元素长度==元素个数
也就是sizeof(arr)/sizeof(arr[0])==3
code;
#include<stdio.h>
int main()
{
int arr[3] = {
1,2,3 };
printf("%d", sizeof(arr) / sizeof(arr[0]));
return 0;
}
运行结果:
一维数组在内存中的存储
We want to understand memory,Let's print the address of the array、
code:
#include<stdio.h>
int main()
{
int arr[3] = {
1,2,3 };
int i = 0;
for (i = 0; i < 3; i++)
{
printf("%p\n", &arr[i]);
}
return 0;
}
运行结果:
由上图可知:
1.一维数组在内存中是连续存放的,You can see that the memory is displayed in hexadecimal,first element bits8与C(12),之间相差4个字节,That is, each element(地址)各占4个字节,difference4个字节
2内存地址.随着数组下标的增长,从低地址增长到高地址.
如下图所示:
*二维数组
二维数组的创建和初始化
A two-dimensional array is one-dimensional On the basis of many a dimension of an array——列,That is, more subscripts.
code:
#include<stdio.h>
int main()
{
int arr[3][4];
return 0;
}
An array with two subscripts like the above is a two-dimensional array.What does the extra subscript he added mean??
看下图:
可以看出,The difference from a one-dimensional array is that there are a few more lines,其他的没什么区别.Then it can be seen from the figure that the first element of this array is subscripted as[0][0]的元素(第0行0列),That is, the element in the upper left corner,第二个元素则是[0][1]以此类推.In fact, it's a bit like a matrix,Students who have studied are easier to understand.
So if you know how to create it, you should initialize it..Initialization and one dimensional array are still a little difference.请看如下代码:
- Unspecified full initialization
#include<stdio.h>
int main()
{
int arr[3][4]={
1,2,3,4,5,6,7,8,9,10,11,12};
return 0;
}
Let's debug the storage situation inside:
由上图可知:
The elements of a two-dimensional array are stored in the same way as a one-dimensional array.,Store from the first element subscript
- Unspecified incomplete initialization
This time let's take an example of a character array to see if it is different from a one-dimensional array.
code
#include<stdio.h>
int main()
{
char arr[3][4] = {
'a','b','c','d','e','f','g','h' };
return 0;
}
1.Two-dimensional arrays are incompletely initialized in the same way as one-dimensional arrays,If all elements are not initialized, fill0,字符数组则补\0.
2.String arrays cannot be written as two-dimensional arrays,char[3][4]=“abcdefg”;这种写法是不存在的.
- Specify the fully initialized
The form of initialization here is different from the initialization of a one-dimensional array..Let's adjust it and we'll understand.
code:
#include<stdio.h>
int main()
{
int arr[3][4] = {
{
1,2,3,4},{
5,6,7,8},{
9,10,11,12} };
return 0;
}
很明显,This initialization method can specify which subscript to initialize,{1,2,3,4}This set corresponds exactly to the0行0,1,2,3列,从而初始化.
- specify incomplete initialization
This initialization method is slightly different from full initialization.
code:
#include<stdio.h>
int main()
{
int arr[3][4] = {
{
1,2},{
5,6},{
9,10} };
return 0;
}
Elements that are not initialized in the specified area are automatically filled0,字符数组则补\0.
Basic use of two-dimensional arrays
Output a two-dimensional array of data:
#include<stdio.h>
int main()
{
int arr[3][4] = {
{
1,2},{
5,6},{
9,10} };
int i = 0;
for (i = 0; i < 3; i++)
{
int j = 0;
for (j = 0; j < 4; j++)
{
printf("arr[%d][%d]=%d ", i, j, arr[i][j]);
}
printf("\n"); //After printing a line at a time,换行
}
return 0;
}
运行结果:
当然,The real use of two-dimensional arrays is definitely not so simple..这个后面会讲到,今天只是了解.
二维数组在内存中的存储
code:
#include<stdio.h>
int main()
{
int arr[3][4] = {
{
1,2},{
5,6},{
9,10} };
int i = 0;
for (i = 0; i < 3; i++)
{
int j = 0;
for (j = 0; j < 4; j++)
{
printf("arr[%d][%d]=%p ", i, j, &arr[i][j]);
}
printf("\n"); //After printing a line at a time,换行
}
return 0;
}
One can see that the dimensional array is stored contiguously in memory,Similarly, two-dimensional arrays are also stored contiguously in memory..difference between each column4个字节,difference between each row16个字节(因为1行4列,4 * 4=16.)So what does this continuous storage do??
1.Another knowledge point here is that the row subscript of a two-dimensional array can be omitted.,Of course, it is not that the line subscript conforms to itself and does not write,It is the number in the line subscript that can be omitted.However, column subscripts cannot be omitted, such as:int arr[][4]; Then the subscript of the line here can be omitted, which is related to the continuous storage of the two-dimensional array.,Because the number of columns in a row is determined, the next row can be found,Because the next element in the last column of this row must be the first column of the next row.,So the line subscript can be omitted.
如图:
*结言
好了,今天的内容就是这些,Learning programming must go to more debugging and observation,总结.希望你有收获,我们下节见!
边栏推荐
- First acquaintance of scrapy framework 1
- 基于 WeihanLi.Npoi 实现excel导入时纯汉字的日期转换
- C语言结构体(入门)
- ETL(二):表达式组件的使用
- Article 48 - Analysis of timestamp2 parameters【2022-08-01】
- js semi-circle loading progress animation js special effects
- 移动端适配,华为浏览器底色无法正常显示
- js true 3d histogram plugin
- 无线振弦采集仪远程修改参数方式
- Basic operations of openGauss database (super detailed)
猜你喜欢
随机推荐
3 ways for OpenFeign to set headers
Summer training camp-week2 graph theory
水平垂直居中方式
SQL Server 2019 installation error 0 x80004005 service there is no timely response to the start or control request a detailed solution
【C语言】剖析函数递归(2)
Automatically generate code generator recommendation-code-gen
最小割和对偶图(未完成)
SQL Server 2014 installation tutorial (nanny-level graphic tutorial)
Mysql索引详解(图文并茂)
RestTemplate use: set request header, request body
80篇国产数据库实操文档汇总(含TiDB、达梦、openGauss等)
FreeRTOS creation tasks - dynamic creation, static creation
C语言结构体(入门)
js semi-circle loading progress animation js special effects
SQL Server database generation and execution of SQL scripts
自媒体创作怎样提高原创度,打造爆款作品?
【C语言】函数哪些事儿,你真的get到了吗?(1)
FreeRTOS--stack experiment
【C语言】函数哪些事儿,你真的get到了吗?(2)
WeChat applet getPhoneNumber interface code=40013