当前位置:网站首页>Recognition of C language array
Recognition of C language array
2022-07-07 01:46:00 【The moon chews into a star~】
Personal card :
Author's brief introduction : A freshman
Personal home page : The moon chews into a star ~
personal WeChat:yx1552029968
Series column : The first step C Language
One sentence per day : Don't be at your best , Fail to live up to your best self
Catalog
1. One dimensional array creation and initialization
1.2 Initialization of an array
1.3 The use of one-dimensional arrays
1.4 One dimensional array storage in memory
2. The creation and initialization of two-dimensional array
2.1 The creation of two-dimensional array
2.2 Initialization of 2D array
2.3 The use of two-dimensional arrays
2.4 Two dimensional array storage in memory
4. Arrays as function arguments
4.1 Wrong design of bubble sort function
4.3 Correct design of bubble sorting function
5.1 Application examples of arrays 1: Sanzi
5.2 Application examples of arrays 2: The Minesweeper game
hello! it's been a long time , The exam is over ! Let me update my knowledge , This time, a large number of knowledge points will be updated , Students who want to get on the bus hurry up !!! Let's continue with the last time , I hope you can support me more , If there is anything to improve, leave more messages in the comment area !
Let's start today's recognition of arrays !
1. One dimensional array creation and initialization
1.1 Array creation
An array is a collection of elements of the same type .
How to create an array :
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
An instance created by an array :
// Code 1
int arr1[10];
// Code 2
int count = 10;
int arr2[count];// Arrays can be created normally ?
// Code 3
char arr3[10];
float arr4[1];
double arr5[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 the concept of variable length arrays .
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 ).
Look at the code :
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 to distinguish , How to allocate in memory .
char arr1[] = "abc";
char arr2[3] = {'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;
}
summary :
1. Arrays are accessed using subscripts , The subscript is from 0 Start .
2. The size of the array can be calculated .
int arr[10];
int sz = sizeof(arr)/sizeof(arr[0]);
1.4 One dimensional array storage in memory
Next, we discuss the storage of arrays in memory .
Look at the code :
#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;
}
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 .
2. The creation and initialization of 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 Initialization of 2D array
// 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 The use of two-dimensional arrays
Two dimensional arrays are also used by subscripts .
Look at the code :
#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 in memory
Like a one-dimensional array , Here we try to print each element of a two-dimensional array .
#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;
}
Through the results, we can analyze , In fact, two-dimensional arrays are also stored continuously in memory .
3. 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 .
#include <stdio.h>
int main()
{
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
int i = 0;
for(i=0; i<=10; i++)
{
printf("%d\n", arr[i]);// When i be equal to 10 When , Cross border visit
}
return 0;
}
Rows and columns of two-dimensional arrays may also be out of bounds .
4. Arrays as function arguments
4.1 Wrong design of bubble sort function
Often when we write code , Will pass the array as an argument to a function , such as : I want to implement a bubble sort ( Here is the idea of algorithm ) The function sorts an integer array .
Then we will use this function :
// Method 1:
#include <stdio.h>
void bubble_sort(int arr[])
{
int sz = sizeof(arr)/sizeof(arr[0]);// Is that right ?
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 i=0;
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;
}
Method 1, have a problem , Let's find a problem , After debugging, you can see bubble_sort intra-function sz , yes 1.
When an array is used as a function parameter , Instead of passing the entire array ?
4.2 What is the array name ?
#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 array name is the address of the first element of the array ( In general )
If the array name is the first element address , that :
int arr[10] = {0};
printf("%d\n", sizeof(arr));
Why the output is :40?
Add :
1. sizeof( Array name ), Calculate the size of the entire array ,sizeof Put a separate array name inside , 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 Correct design of bubble sorting function
When an array passes parameters , In fact, it just passes the address of the first element of the array .
So even if it is written as an array in the parameter part of the function : int arr[] It still represents a pointer : int *arr .
that , intra-function sizeof(arr) The result is 4.
If Method 1 In the wrong , How to design ?
// Method 2
void bubble_sort(int arr[], int sz)// Parameter to receive the number of array elements
{
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 i=0;
int arr[] = {3,1,7,5,8,9,0,2,4,6};
int sz = sizeof(arr)/sizeof(arr[0]);
bubble_sort(arr, sz);// Whether it can be sorted normally ?
for(i=0; i<sz; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
5. Array instance :
5.1 Application examples of arrays 1: Sanzi
5.2 Application examples of arrays 2: The Minesweeper game
These two examples will be explained in detail in a special topic later , Hope not to miss it !!!
Good da , Today's content is here , Thank you for watching , Thank you. , I hope you can support me more , Let's study together , Progress together !!!
Thanks for watching !
边栏推荐
- mongodb查看表是否导入成功
- AI automatically generates annotation documents from code
- How can I code for 8 hours without getting tired.
- LeetCode. 剑指offer 62. 圆圈中最后剩下的数
- mysqlbackup 还原特定的表
- Baidu flying general BMN timing action positioning framework | data preparation and training guide (Part 1)
- 百度飞将BMN时序动作定位框架 | 数据准备与训练指南 (上)
- Make DIY welding smoke extractor with lighting
- 盒子拉伸拉扯(左右模式)
- [signal and system]
猜你喜欢
Comparison of picture beds of free white whoring
1123. The nearest common ancestor of the deepest leaf node
Yunna | work order management measures, how to carry out work order management
JVM 内存模型
Gin introduction practice
JVM memory model
LeetCode:1175. Prime permutation
Make DIY welding smoke extractor with lighting
AcWing 361. Sightseeing cow problem solution (SPFA seeking positive ring)
制作带照明的DIY焊接排烟器
随机推荐
The difference between Tansig and logsig. Why does BP like to use Tansig
Vocabulary in Data Book
mysqlbackup 还原特定的表
AcWing 1148. 秘密的牛奶运输 题解(最小生成树)
使用nodejs完成判断哪些项目打包+发版
JVM 内存模型
Right mouse button customization
Appium自动化测试基础 — uiautomatorviewer定位工具
JVM memory model
一起看看matlab工具箱内部是如何实现BP神经网络的
拖拽改变顺序
字符串的相关编程题
736. LISP syntax parsing: DFS simulation questions
鼠标右键 自定义
Blue Bridge Cup 2022 13th provincial competition real topic - block painting
Ds-5/rvds4.0 variable initialization error
js如何快速创建一个长度为 n 的数组
各种语言,软件,系统的国内镜像,收藏这一个仓库就够了: Thanks-Mirror
Drag to change order
Machine learning: the difference between random gradient descent (SGD) and gradient descent (GD) and code implementation.