当前位置:网站首页>[dish of learning notes dog learning C] array
[dish of learning notes dog learning C] array
2022-07-21 03:15:00 【Jiang Junzhu】
List of articles
One 、 One dimensional array
1. One dimensional array creation
int main() {
int arr[8];// An array type Array name [ Element number ];
char ch[3];
return 0;
}
notes : The number of elements must be a constant expression , Form like arr[n] The array of is created only in C99 Only supported under syntax , Some compilers can compile such code , But not recommended .
2. One dimensional array initialization
int main() {
int arr1[3] = {
1, 2, 3 };// Fully initialized
int arr2[3] = {
1, 2 };// Incomplete initialization
/* The following two lines of code are equivalent int arr3[3] = { 1, 2, 3 }; int arr3[] = { 1, 2, 3 }; */
return 0;
}
Pay attention to distinguish the following codes :
int main() {
char ch1[5] = {
'b', 'i', 't' };
char ch2[] = {
'b', 'i', 't' };
char ch3[5] = "bit";// b i t \0 0
char ch4[] = "bit";// b i t \0
return 0;
}
because \0
It's the end sign , and ch2
did not \0
, So it will continue to print , Until I find \0
until .
3. One dimensional array memory storage
int main() {
int arr[5];
char ch[5];
int i = 0;
for (i = 0; i < 5; i++) {
printf("arr[%d] = %p ", i, &arr[i]);//%p - Print in address format - Hexadecimal printing
printf("ch[%d] = %p\n", i, &ch[i]);
}
return 0;
}
As can be seen from the figure above , Every arr The size difference between the addresses of elements 4 Bytes , Every ch The size difference between the addresses of elements 1 Bytes . And one int The size of the type is just 4 byte , One char The size of the type is just 1 byte . therefore One dimensional arrays are stored continuously in memory , also As the address grows , The address changes from low to high .
notes : The address of the array name is the address of the first element of the array .
Two 、 Two dimensional array
1. The creation of two-dimensional array
int main() {
// establish
// An array type Array name [ Number of line elements ][ Number of column elements ];
int arr[3][4];
char ch[3][4];
return 0;
}
2. 2D array initialization
int main() {
// initialization - Create and assign values at the same time
int arr1[3][4] = {
1,2,3,4,5,6,7,8,9,10,11,12 };
int arr2[3][4] = {
1,2,3,4,5,6,7 };// Incomplete initialization - Back up 0
int arr3[3][4] = {
{
1, 2}, {
3, 4}, {
5, 6} };
int arr4[][4] = {
{
1, 2}, {
3, 4}, {
5, 6} };// Lines can be omitted , But the column cannot be omitted
return 0;
}
notes : For code Readability
, It is recommended to use one for each line {}
Cover up , It can be understood that a two-dimensional array is understood as a one-dimensional array with several groups of one-dimensional arrays .
3. Memory storage of two-dimensional array
int main() {
int arr[3][4] = {
{
1,2},{
3,4},{
5,6} };
int i = 0;
int j = 0;
for (i = 0; i < 3; i++) {
for (j = 0; j < 4; j++) {
printf("arr[%d][%d] = %p\n", i, j, &arr[i][j]);
}
}
return 0;
}
As can be seen from the figure above , Every arr The size difference between the addresses of elements is still 4 Bytes , The last element of the first line and the first element of the second line The size difference between them is also 4. therefore Two dimensional arrays are also stored continuously in memory , It's also As the address grows , The address changes from low to high .
边栏推荐
- webSocket學習與使用
- Apache Doris Binlog Load使用方法及示例
- 5-FU/DEX-g-PLA纳米微粒/BSA-AgNCs-PEI纳米粒/Cu(DDC)2蛋白纳米粒的制备
- LabVIEW中的自动保存功能
- win10如何实现电脑上文件共享访问
- Niuke bm6 judges whether there is a ring in the linked list
- P7354 「PMOI-1」骑士の棋
- Basic syntax of symbol, iterator and generator in ES6
- 试题 F: 统计子矩阵
- SQL optimization (x): association update
猜你喜欢
Visual Studio 开发环境的配置
Vben admin time selector related configuration and setting of unselectable time
easyExcel设置最后一行的样式【可以拓展为每一行】
作为程序员的思考
毕业季--各应用场景案例使用技术
【C】 C语言入门
【学习笔记之菜Dog学C】数据存储
[dish of learning notes dog learning C] function recursion
Mysql, I'll create 200W pieces of data casually and tell you about paging optimization.
ES6中Symbol、迭代器和生成器基本语法
随机推荐
[dish of learning notes, dog learning C] getting to know the pointer for the first time
2022-7-19 第八小组 顾宇佳 学习笔记 (this关键字和封装)
(洛谷)P1605迷宫(一入深搜苦如海,从此时间是大die)
【C】 C语言入门
【学习笔记之菜Dog学C】链式访问、函数的声明和定义、goto语句
基于SSM实现水果蔬菜商城管理系统
【学习笔记之菜Dog学C】函数递归
30 spark introduction: Spark Technology stack explanation, partition, system architecture, operator and task submission method
【学习笔记之菜Dog学C】数据存储
Implementation of fruit and vegetable mall management system based on SSM
Apache Doris ODBC appearance database mainstream version and its ODBC version correspondence
(第十三届蓝桥杯)试题 A: 九进制转十进制
【学习笔记之菜Dog学C】扫雷游戏
【学习笔记之菜Dog学C】数组
[scientific literature measurement] statistics and visualization of word number and frequency of Chinese and English literature titles and abstracts
归并排序/快速排序
LabVIEW中的自动保存功能
2022-07-19-利用shell去激活conda环境
Apache Doris ODBC外表数据库主流版本及其ODBC版本对应关系
[dish of learning notes dog learning C] function