当前位置:网站首页>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 !
边栏推荐
- Domestic images of various languages, software and systems. It is enough to collect this warehouse: Thanks mirror
- First experience of JSON learning - the third-party jar package realizes bean, list and map to create JSON format
- 一文带你走进【内存泄漏】
- 黑马笔记---创建不可变集合与Stream流
- Vocabulary in Data Book
- Get to know MySQL for the first time
- Related programming problems of string
- Set up [redis in centos7.x]
- JS reverse -- ob confusion and accelerated music that poked the [hornet's nest]
- AcWing 904. Wormhole solution (SPFA for negative rings)
猜你喜欢
LeetCode:1175. 质数排列
Transplant DAC chip mcp4725 to nuc980
LeetCode. Sword finger offer 62 The last remaining number in the circle
Dark horse notes - exception handling
tansig和logsig的差异,为什么BP喜欢用tansig
修改px4飞控的系统时间
454 Baidu Mianjing 1
AcWing 361. Sightseeing cow problem solution (SPFA seeking positive ring)
The cradle of eternity
shell脚本快速统计项目代码行数
随机推荐
Google released a security update to fix 0 days that have been used in chrome
uva 1401 dp+Trie
制作带照明的DIY焊接排烟器
What does front-end processor mean? What is the main function? What is the difference with fortress machine?
场景实践:基于函数计算快速搭建Wordpress博客系统
Today's question -2022/7/4 modify string reference type variables in lambda body
uva 1401 dp+Trie
JVM memory model
Get to know MySQL for the first time
Dark horse notes - exception handling
The cradle of eternity
C语言实例_2
Amway wave C2 tools
Clickhouse fields are grouped and aggregated, and SQL is queried according to the granularity of any time period
Use nodejs to determine which projects are packaged + released
AcWing 1148. 秘密的牛奶运输 题解(最小生成树)
刨析《C语言》【进阶】付费知识【二】
Analyze "C language" [advanced] paid knowledge [End]
AcWing 346. Solution to the problem of water splashing festival in the corridor (deduction formula, minimum spanning tree)
Mongodb checks whether the table is imported successfully