C语言(七):二维数组
分享兴趣,传播快乐,
增长见闻,留下美好!
亲爱的您,这里是LearningYard新学苑。
今天小编为大家带来的是C语言(七):二维数组。
Share interests, spread happiness,
increase knowledge, and leave good!
Dear you, this is the Learning Yard.
Today, Xiaobian brings you
C language (7): two-dimensional array.
01
数组补充(1)关于上一节“数组维度不能定义变量”的问题现做出另一种解释,从目前来看C99标准中支持可变维度
%c:只输出一个字符
(1) Another explanation is given for the problem of "array dimension cannot define variables" in the previous section. At present, the C99 standard supports variable dimension% c: only one character is output
(2)关于数组越界问题,某些编译器可以运行(不同编译器执行的标准不同)
(2) Some compilers can run on array overruns (different compilers implement different standards)
02
字符串处理函数(1)字符数组
char str[];
①初始化每个元素
②使用字符串常量初始化字符数组可以省略大括号
(1) Character array
char str[];
① Initialize each element
② Use the string constant to initialize the character array You can omit the braces
(2)字符串处理函数
①尽量使用标准库
②
·获取字符串的长度:strlen(不包含'0')
·获取字符串的尺寸:sizeof(包含'0')
·拷贝字符串:strcpy和strncpy
strcpy(str1,str2):将str1拷贝为str2(受限,若str1长度小于str2,那么无法全部拷贝,但编译时不会报错,但执行时因系统不同会出现不同结果)
(2) String processing function
① try to use the standard library
② · get the length of the string: strlen (excluding ' 0') · get the size of the string: sizeof (including ' 0') · copy the string: strcpy and strncpystrcpy (str1, str2): copy str1 to str2 (limited, if the length of str1 is less than str2, you cannot copy all of them, but no error will be reported during compilation, but different results will occur due to different systems during execution)
strncpy:可以指定拷贝的字符数,但是遇'0'才会停止
Strncpy: You can specify the number of characters to copy, but it will stop when it encounters' 0 '
·连接字符串:strcat和strncat
使用strncat需要自己追加'0'
·Connection strings: strcat and strncat
Using strncat requires appending ' 0'
(3)比较字符串:strcmp和strncmp
使用strncmp函数可以给定一个参数,只比较前面n个字符
(3) Compare strings: strcmp and strncmp
Use the strncmp function to give a parameter and compare only the first n characters
03
二维数组
(1)二维数组通常称为矩阵
(1) A two-dimensional array is often called a matrix
(2)二维数组的定义
·类型 数组名[常量表达式][常量表达式]
例如:int a[6][5]; 6行5列
char b[4][5]; 4行5列
二维数组在内存中的存储形式:
(2) Definition of two-dimensional array
·Type array name [constant expression] [constant expression]
For example: int a [6] [5]; 6 rows and 5 columns
char b [4] [5]; 4 rows and 5 columns
Storage form of two-dimensional array in memory:
(3)二维数组的访问
·数组名[下标][下标]
a[1][3]; 访问a数组中的第二行第四列的元素
·注意下标的取值范围
比如int a[3][4],行下标的取值范围为0~2,列下标的取值范围为0~3
(3) Access to two-dimensional arrays
·Array name [subscript] [subscript]
a[1][3]; Access the elements in the second row and fourth column of the a array
·Pay attention to the value range of subscripts
For example, int a [3] [4], the value range of row subscripts is 0~2, and the value range of column subscripts is 0~3
(4)二维数组的初始化
·可以将所有的数据写在一个括号内
(4) Initialization of two-dimensional array
·All data can be written in a bracket
·可以用大括号将每一行的元素括起来
·You can enclose the elements of each line with braces
int a[3][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};
·对部分元素赋值
·Assign values to some elements
int a[3][4]={{1},{2},{3}};
表示将每一行的第一个元素分别赋值为1,2,3
It means that the first element of each row is assigned 1, 2, 3
·将二维数组整个初始化为0
·Initialize the entire two-dimensional array to 0
int a[3][4]={0};
·指定初始化元素,未被初始化的自动赋值为0
·Specifies the initialization element. The uninitialized automatic assignment is 0
int a[3][4]={[0][0]=1,[1][1]=1}
·第1维的元素个数可以不写,其他维度必须写上
·The number of elements in the first dimension can not be written, and other dimensions must be written
int a[][4]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};
END
今天的分享就到这里了。
如果您对今天的文章有独特的想法,
欢迎给我们留言,
让我们相约明天,
祝您今天过得开心快乐!
That's all for today's sharing.
If you have unique ideas about today's article,
please leave us a message.
Let's meet tomorrow and wish you a happy day!
翻译:百度翻译
参考:《零基础入门学习C语言:带你学C带你飞》、哔哩哔哩小甲鱼视频、网络图片
声明:本文由LearningYard新学苑原创,若有侵权请联系删除!
关注我们
文案&排版:易春秀
审核:闫庆红