当前位置:网站首页>Array of C language
Array of C language
2022-07-27 17:38:00 【Cabbage 00】
Catalog
One dimensional array creation and initialization
The way one-dimensional arrays are created
The use of one-dimensional arrays
One dimensional array storage in memory
The creation and initialization of two-dimensional array
The creation of two-dimensional array
initialization —— Assign value to it while creating
The use of two-dimensional arrays
Storage of two-dimensional array in array
One dimensional array creation and initialization
Array : A collection of elements of the same type
The way one-dimensional arrays are created

// Array initialization instance
int arr1[3];
char arr2[8];
double arr3[20];Be careful : Array creation ,[] You have to give a constant in the , You can't use variables
Initialization of an array
The initialization of an array refers to , While creating the array, give some reasonable initial values to the contents of the array ( initialization )
// Fully initialized
int arr1[10] = {1,2,3,4,5,6,7,8,9,10};
// Incomplete initialization ( The array length is 10, after 5 A complement 0)
int arr2[10] = { 1,2,3,4,5 };
// Determine the initialization of the array length according to the initialization content
int arr3[] = {1,2,3,4,5,6};// The array length is 6
// The first way to write a character array
char ch1[5] = "bit";// Incomplete initialization , Later use '\0' Add
char ch2[] = "bit";// The array length is 3
// The second way to write a character array
char ch3[] = { 'b','i','t' };// altogether 3 Elements
char ch4[6] = { 'b','i','t' };// altogether 6 Elements , For the other '\0'The use of one-dimensional arrays
[]: Subscript reference operator , Access elements in an array by subscript , Array index from 0 Start
#include <stdio.h>
int main() {
int arr1[10] = { 0 };
arr1[4] = 5;// Will array the 5 The number is changed to 5
int sz = sizeof(arr1) / sizeof(arr1[0]);
// Traversal array
int i = 0;
for (i = 0; i < sz; i++) {
printf("%d\n", arr1[i]);
}
return 1;
}One dimensional array storage in memory
- Each element of a one-dimensional array is stored continuously in memory
- As the array subscript grows , The address changes from low to high
- The array name is the address of the first element
The creation and initialization of two-dimensional array
The creation of two-dimensional array

initialization —— Assign value to it while creating
// Fully initialized
int arr1[3][4] = {1,2,3,4,5,6,7,8,9,10,11,12};
// Incomplete initialization —— Back up 0
int arr2[3][4] = {1,2,3,4,5,6,7};
// The second method
int arr3[3][4] = { {1,2},{3,4},{4,5} };// Not completely initialized here , The last two columns are supplemented 0
// The second method is to determine the number of rows according to the following elements , But there are several elements in a line that cannot be omitted
int arr3[][4] = { {1,2},{3,4},{4,5} };The use of two-dimensional arrays
The operation of two-dimensional array is also through subscript [][]
#include <stdio.h>
int main() {
// Traversing a two-dimensional array
int arr1[][4] = { {1,2},{3,4},{4,5} };
int i = 0;
int j = 0;
for (i = 0; i < 3; i++) {
for (j = 0; j < 4; j++) {
printf("%d ", arr1[i][j]);
}
printf("\n");
}
return 1;
}Storage of two-dimensional array in array
- Two dimensional arrays are also stored continuously in memory ( A row is internally continuous , Line breaks are also continuous )
- A two-dimensional array stored in memory is a very long one-dimensional array
#include <stdio.h>
int main() {
// Traversing a two-dimensional array
int arr1[][4] = { {1,2},{3,4},{4,5} };
int i = 0;
int* p = &arr1[0][0];
for (i = 0; i < 12; i++) {
printf("%d ", *p);
p++;
}
return 1;
}Arrays as function arguments
When an array passes parameters, it passes the address of the first element of the array
The array name is the address of the first element of the array, but it has 2 Exceptions
- sizeof( Array name )—— The array name indicates that the size of the entire array is calculated, and the unit is bytes
- & Array name —— The array name represents the entire array , It takes out the address of the entire array
In addition, all array names are the addresses of the first elements of the array
#include <stdio.h>
int main() {
int arr[10] = { 0 };
printf("%p\n", arr);
printf("%p\n", arr+1);// Bad 4
printf("%p\n", &arr);
printf("%p\n", &arr+1);// Bad 40
return 1;
}#include <stdio.h>
void main() {
int a[] = { 1,2,3,4 };
printf("%d\n", sizeof(a));//16—— The size of the entire array
printf("%d\n", sizeof(a+0));//4—— The address of the first element
printf("%d\n", sizeof(*a));//4—— The first element of the array
printf("%d\n", sizeof(a+1));//4—— The address of the second element
printf("%d\n", sizeof(a[1]));//4—— The second element of the array
//&a
printf("%d\n", sizeof(&a));//4—— Address of array , The address is generally 4(32 position )
printf("%d\n", sizeof(*&a));//16—— The size of the entire array
printf("%d\n", sizeof(&a+1));//4—— It's an address ( The address of the space behind the array ), The address is generally 4(32 position )
printf("%d\n", sizeof(&a[0]));//4—— The address of the first element of the array
printf("%d\n", sizeof(&a[0]+1));//4—— The address of the second element of the array
}character string
Definition : A string of characters enclosed by double quotation marks is called the literal value of the string , Or string , It's an array
Be careful : The end of a string is a \0 The escape character of , When calculating the length of a string \0 It's the end sign , Does not count as the contents of the string .
What is wrapped in single quotation marks is a character constant , What is wrapped in double quotation marks is a string, which can wrap multiple characters ;
int main() {
char a[] = "abc";
char b[] = { 'a','b','c' };
printf("%s\n", a);
printf("%s", b);
}Print the results :

reason : The end flag of the string is \0,b No, I didn 't \0 Will continue to print backwards until you find \0
边栏推荐
- Shell programming specifications and variables
- 苹果官网罕见打折,iPhone13全系优惠600元;国际象棋机器人弄伤对弈儿童手指;国内Go语言爱好者发起新编程语言|极客头条...
- Understand the staticarea initialization logic of SAP ui5 application through the initialization of fileuploader
- 数据库超话(二)
- Smart fish tank design based on stm32
- 20 years ago, he was Ma Yun's biggest enemy
- What are VO, do, dto and Po
- Mysql: function
- Oracle-Linux-7.9是否能支持Oracle-19c的ACFS文件系统?
- Day 7 summary & homework
猜你喜欢

Microsoft silently donated $10000 to curl, which was not notified until half a year later

【cf】#681 A. Kids Seating (Div. 2, based on VK Cup 2019-2020 - Final)

Global string object (function type) +math object

Three table joint query 3

7 岁男孩被 AI 机器人折断手指,仅因下棋太快?

一个端到端的基于 form 表单的文件上传程序,包含客户端和服务器端

Motion capture system for end positioning control of flexible manipulator

.NET Core with 微服务 - 什么是微服务

With the arrival of large displacement hard core products, can the tank brand break through the ceiling of its own brand?

How to extract tables from PDF through C /vb.net
随机推荐
Gods at dusk, "cat trembles" bid farewell to the big V Era
数据库超话(三)
Dense optical flow extraction dense_ Flow understanding
Gartner authority predicts eight development trends of network security in the next four years
Database hyperphone (4)
helm安装kubevela完整Makefile脚本内容
SVM+Surf+K-means花朵分类(Matlab)
【obs】NewSocketLoopEnable 网络优化
About paths mapping in SAP ui5 application ui5.yaml
Flex flex flex box layout 2
小于n的最大数
Database hyperphone (II)
Svm+surf+k-means flower classification (matlab)
Switch and router technology-02-working principle of Ethernet switch
The chess robot broke the finger of a 7-year-old boy. Netizen: it violated the first law of robots
科目三: 直线行驶
Gree "not cool": the giant lawsuit ended and was reduced by large dealers. Is it too late for the new battlefield of air conditioning?
交换机和路由器技术-02-以太网交换机工作原理
2021-06-18 automatic assembly error in SSM project
Project exercise: the function of checking and modifying tables