当前位置:网站首页>C language - array
C language - array
2022-07-18 22:12:00 【zeroGho】
Catalog
1.2 Initialization of an array
1.3 The use of one-dimensional arrays
1.4 Storage of one-dimensional array in memory
2.1 The creation of two-dimensional array
2.3 Two dimensional arrays use
2.4 Two dimensional array storage
Four 、 Arrays as arguments to functions
One 、 One dimensional array
1.1 Array creation
An array is a collection of elements of the same type ;
Array creation
type_t arr_name [const_n];
//type_t Is the element type of the exponential group
//const_n Is a constant expression , Used to specify the size of an array
int arr1[10];
char arr2[10];
float arr3[1];
double arr4[20];
notes : Array creation , stay C99 Before standard , [ ] You have to give a constant in the , You can't use variables . stay C99 The standard supports variable length numbers The concept of group , The size of the array can be specified by variables , But arrays cannot be initialized .
1.2 Initialization of an array
The initialization of an array refers to , While creating an array, give the contents of the array some reasonable initial values ( initialization ).
for example :
int arr1[10] = {1,2,3};
int arr2[] = {1,2,3,4};
int arr3[5] = {1,2,3,4,5};
char arr4[3] = {'a',98, 'c'};
char arr5[] = {'a','b','c'};
char arr6[] = "abcdef";If you want to create an array without specifying the size of the array, you have to initialize it . The number of elements in the array is determined according to the contents of initialization .
But for the following code :
char arr1[] = "abc";
char arr2[3] = {'a','b','c'};Although the character array is initialized to abc , But arrays 1 During initialization, there are actually 4 Elements , Respectively "a" "b" "c" "\0" , And arrays 2 There are three elements in “a" "b" "c" .
1.3 The use of one-dimensional arrays
For the use of arrays, we introduced an operator : [] , Subscript reference operator . It's actually an array access operator . So let's look at the code :
#include <stdio.h>
int main()
{
int arr[10] = {0};// Incomplete initialization of arrays
// Count the number of elements in an array
int sz = sizeof(arr)/sizeof(arr[0]);
// Assign values to the contents of the array , Arrays are accessed using subscripts , Subscript from 0 Start . therefore :
int i = 0;// Do subscripts
for(i=0; i<10; i++)// Write here 10, Ok or not ?
{
arr[i] = i;
}
// Output the contents of the array
for(i=0; i<10; ++i)
{
printf("%d ", arr[i]);
}
return 0;
}
The output of this code is :0 1 2 3 4 5 6 7 8 9
summary : 1. Arrays are accessed using subscripts , The subscript is from 0 Start .
2. The size of the array can be calculated .
1.4 Storage of one-dimensional array in memory
We can check the address of the array through the code , as follows :
#include <stdio.h>
int main()
{
int arr[10] = {0};
int i = 0;
int sz = sizeof(arr)/sizeof(arr[0]);
for(i=0; i<sz; ++i)
{
printf("&arr[%d] = %p\n", i, &arr[i]);
}
return 0;
}
The operation results are as follows :

Watch the output carefully , We know , As the array subscript grows , Address of element , It's also increasing regularly . So we can draw a conclusion : Arrays are continuously stored in memory .
Two 、 Two dimensional array
2.1 The creation of two-dimensional array
// Array creation
int arr[3][4];
char arr[3][5];
double arr[2][4];2.2 2D array initialization
// Array initialization
int arr[3][4] = {1,2,3,4};
int arr[3][4] = {
{1,2},{4,5}};
int arr[][4] = {
{2,3},{4,5}};// If the two-dimensional array is initialized , Lines can be omitted , Columns cannot be omitted 2.3 Two dimensional arrays use
Similar to one-dimensional array , Two dimensional arrays are also accessed through subscripts
#include <stdio.h>
int main()
{
int arr[3][4] = {0};
int i = 0;
for(i=0; i<3; i++)
{
int j = 0;
for(j=0; j<4; j++)
{
arr[i][j] = i*4+j;
}
}
for(i=0; i<3; i++)
{
int j = 0;
for(j=0; j<4; j++)
{
printf("%d ", arr[i][j]);
}
}
return 0;
}2.4 Two dimensional array storage
#include <stdio.h>
int main()
{
int arr[3][4];
int i = 0;
for(i=0; i<3; i++)
{
int j = 0;
for(j=0; j<4; j++)
{
printf("&arr[%d][%d] = %p\n", i, j,&arr[i][j]);
}
}
return 0;
}
The operation results are as follows :

Through the results, we can analyze , In fact, two-dimensional arrays are also stored continuously in memory .
3、 ... and 、 An array
The subscript of an array is range limited .
The next stipulation of the array is from 0 At the beginning , If the array has n Elements , The subscript of the last element is n-1.
So if the subscript of the array is less than 0, Or greater than n-1, The array is accessed out of bounds , Access beyond the legal space of the array .
C The language itself does not check the bounds of array subscripts , The compiler does not necessarily report an error , But the compiler does not report an error , That doesn't mean the program is right , So when programmers write code , You'd better do cross-border inspection yourself
Four 、 Arrays as arguments to functions
4.1 Bubble sort ( error )
Often when we write code , Will pass the array as an argument to a function , such as : I want to implement a bubble sort function Sort an integer array .
#include <stdio.h>
void bubble_sort(int arr[])
{
int sz = sizeof(arr)/sizeof(arr[0]);
int i = 0;
for(i=0; i<sz-1; i++)
{
int j = 0;
for(j=0; j<sz-i-1; j++)
{
if(arr[j] > arr[j+1])
{
int tmp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tmp;
}
}
}
}
int main()
{
int arr[] = {3,1,7,5,8,9,0,2,4,6};
bubble_sort(arr);// Whether it can be sorted normally ?
for(i=0; i<sizeof(arr)/sizeof(arr[0]); i++)
{
printf("%d ", arr[i]);
}
return 0;
}actually , This code cannot realize bubble sorting , Why? ?
Through debugging, we can find that inside the function , We can calculate sz=1, Here we will mention the transfer method of array as a function parameter .
4.2 Array name
Let's start with a piece of code :
#include <stdio.h>
int main()
{
int arr[10] = {1,2,3,4,5};
printf("%p\n", arr);
printf("%p\n", &arr[0]);
printf("%d\n", *arr);
// Output results
return 0;
}The result is :

We can come to a conclusion :
The array name is the address of the first element
however , We also use the array name when calculating the size of the array , It seems that the array size can be output normally .
int arr[10] = {0};
printf("%d\n", sizeof(arr));The result is :40
notes :
1. sizeof( Array name ), Calculate the size of the entire array ,sizeof Put a separate array name inside , At this point, the array name represents the entire array .
2. & Array name , What we get is the address of the array .& Array name , The array name represents the entire array .
In addition to this 1,2 Except for two cases , All array names represent the address of the first element of the array
4.3 Bubble sort ( correct )
Now let's write the correct bubble sort code :
void bubble_sort(int arr[], int sz)// Parameter to receive the number of array elements
{
for(i=0; i<sz-1; i++)
{
int j = 0;
for(j=0; j<sz-i-1; j++)
{
if(arr[j] > arr[j+1])
{
int tmp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = tmp;
}
}
}
}
int main()
{
int arr[] = {3,1,7,5,8,9,0,2,4,6};
int sz = sizeof(arr)/sizeof(arr[0]);
bubble_sort(arr, sz);
for(i=0; i<sz; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
边栏推荐
- 21届毕业生毕业一年内的真实工作状态
- LeetCode 第23天
- Downlink power distribution technology of synaesthesia integrated system
- Lecture 4: round table seats
- LETV has become the king of anti involution: employees have lived a fairy life without 996!
- 1302_FreeRTOS中CoRoutine设计实现分析
- 6G通感一体化组网融合技术
- How many QPS are your interfaces?
- Comparison and reference of collaborative office system (OA system) selection in 2022
- Key technologies and challenges of communication perception integration
猜你喜欢

Cmu15445 (fall 2019) project 4 - logging & Recovery details

信息收集

降噪蓝牙耳机哪个好?蓝牙耳机降噪推荐

Linux 解决 Oracle :ORA-12537: TNS:connection closed(连接关闭)问题

Other new features of MySQL MySQL 8
![[pictures and texts] U-disk startup disk production U-disk startup disk reinstallation system tutorial](/img/9b/d5839cd0f7a0558cc583e9e6ae832d.png)
[pictures and texts] U-disk startup disk production U-disk startup disk reinstallation system tutorial

Publication en niveaux de gris istio: déploiement du projet de microservice bookinfo

Low code development builds business process management solutions

PyQt5-文件对话框(QFileDialog)

The real working state of the 21st graduates within one year after graduation
随机推荐
【C】 Dynamic memory management
How many QPS are your interfaces?
1302_ Analysis of design and implementation of coroutine in FreeRTOS
Personal information protection based on classification and classification
为什么mysql的count()方法这么慢?
机器学习实战运用:速刷牛客5道机器学习题目
openGauss数据库
Comparison and reference of collaborative office system (OA system) selection in 2022
Spark Streaming 编程指南
Leetcode day 23
The upgraded ranking activity is hot again. Looking around, it's full of bonuses
MP1655GG-Z MPS/美国芯源 MOSFET 的开关模式转换器 资源 方案
Win11老是弹出输入体验怎么办
网络空间对抗防御中的智能监测技术研究
哪个品牌的蓝牙耳机降噪好?主动降噪耳机排行榜10强
Leetcode 1331. Array sequence number conversion
Vs2017\vs2019\vs2022 project redundant files (intermediate files \ temporary files) one click clean bat
Caractéristiques et mode d'utilisation de la batterie ni MH (mécanisme de charge de la batterie ni MH FDK)
容器内部查看服务状态报错:Failed to get D-Bus connection: Operation not permitted
Flutter 卡在 Running Gradle task ‘assembleDebug‘... 的解决方法