当前位置:网站首页>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
边栏推荐
- KMP模板——字符串匹配
- VO、DO、DTO、PO是什么
- Functions in JS
- Rare discounts on Apple's official website, with a discount of 600 yuan for all iphone13 series; Chess robot injured the fingers of chess playing children; Domestic go language lovers launch a new pro
- 20年前,他是马云最大的敌人
- Chery omenda is also too similar to Chang'an uni-t, but does it look like it? Is the product power like it?
- 2021-06-18 automatic assembly error in SSM project
- An analysis of CPU explosion of a smart logistics WCS system in.Net
- Shell编程规范与变量
- 大排量硬核产品来袭,坦克品牌能否冲破自主品牌天花板?
猜你喜欢

Chery omenda is also too similar to Chang'an uni-t, but does it look like it? Is the product power like it?

Node package depends on download management

信通院陈屹力:降本增效是云原生应用最大价值

Global string object (function type) +math object

Select structure

Technical practice dry goods | from workflow to workflow

The chess robot broke the finger of a 7-year-old boy. Netizen: it violated the first law of robots

Kubernetes第七篇:使用kubernetes部署prometheus+grafana监控系统(Kubernetes工作实践类)

三表联查3

Explain the pile of binary trees in detail
随机推荐
一文理解分布式开发中的服务治理
Switch and router technology-02-working principle of Ethernet switch
国产新冠口服药为什么是治艾滋病的药
SVM+Surf+K-means花朵分类(Matlab)
信通院陈屹力:降本增效是云原生应用最大价值
With the arrival of large displacement hard core products, can the tank brand break through the ceiling of its own brand?
App crash collection and analysis
Microsoft silently donated $10000 to curl, which was not notified until half a year later
$attrs与$listeners组件传值
关于 SAP UI5 应用 ui5.yaml 里的 paths 映射问题
二舅的外甥和他的学生们
Sharing of local file upload technology of SAP ui5 fileuploader
Neural network implementation of handwritten numeral classification matlab
Xcode releases test package testflight
Node package depends on download management
MySQL: Functions
Swift QQ authorized login pit set
New attributes of ES6 array
【单片机】2.2 AT89S52的引脚功能
2021-06-18 automatic assembly error in SSM project