当前位置:网站首页>C language from entry to Earth - array
C language from entry to Earth - array
2022-07-24 07:06:00 【Bubble milk】
Personal home page : Bubble milk
Series column :C Language from entry to soil
If you want to know more about C The content of language , Just Focus on my special column Well , I'm sure you can learn more interesting knowledge from it , Towards goals and dreams , Come on

List of articles
1. One dimensional array creation and initialization
1.1 Array creation
Q: What is? Array ?
A: A group of The same type Elemental aggregate
Q: How arrays are created ?
type arr_name[const_n]; //type Is the element type of the exponential group //const_n Constant expression , Used to determine the size of the array
for example :
// Code 1
int arr1[10];
// Code 2
int count = 10;
int arr2[count];
// Code 3
char arr3[10];
float arr4[10];
double arr5[10];
notes : In the use of Code 2 When creating an array , Pay attention to whether the compiler you use supports C99 standard , No support for C99 Standard compiler , [] Only one Constant , You can't put variables . For arrays like this , We usually call it Variable length array .
1.2 Initialization of an array
The initialization of array refers to , Assign values to the contents of the array while creating the array .
for example :
int arr1[10] = {
0 };
int arr2[10] = {
1,2,3 };
int arr3[] = {
1,2,3 };
char arr4[3] = {
'a','b','c' };
char arr5[] = {
'a',98,'c' };
char arr6[] = "abcdef";
reflection :
Array is not initialized , What is inside the array ?
When initializing an array , Initialize only the first digit , What is inside the array ?
Press above arr6 Error initializing character array , How many characters are there ?
problem 1 :
Array is not initialized , What is inside the array ?

obviously , If the array is not initialized , The contents of the array are all indefinite
problem 2 :
When initializing an array , Initialize only the first digit , What is inside the array ?

ordinary F10 debugged , You can see , The first digit is the initialized value , The rest defaults to 0, For such initialization , We also call it —— Incomplete initialization .
problem 3 :
Press above arr6 Error initializing character array , How many characters are there ?

You can see , Use " " To create a string , There will be one more at the end '\0'
1.3 The use of one-dimensional arrays
before this , We are going to invite an old friend (づ ̄ 3 ̄)づ
[]Subscript reference operator .
#include <stdio.h>
int main()
{
int arr[10] = {
0 };// Incomplete initialization
int sz = sizeof(arr) / sizeof(arr[0]);// Find the number of elements of the array
int i = 0;
// Assign values to the array by input
for (i=0; i<10; ++i)
{
scanf("%d", &arr[i]);
}
// Output each element of the array
for (i=0; i<sz; ++i)
{
printf("%d ", arr[i]);
}
return 0;
}
Summary :
- The array is accessed by subscript , Subscript from 0 Start
- The number of elements of the array can be calculated

2. The creation and initialization of two-dimensional array
2.1 The creation of two-dimensional array
int arr1[3][4];
char arr2[3][4];
double arr3[][4];
When creating a two-dimensional array , Lines can be omitted , Columns cannot be omitted , That is, the second
[]There must be specific Constant expression

2.2 Initialization of 2D array
int arr1[3][4] = {
1,2,3,4 };// Incomplete initialization
int arr2[3][4] = {
{
1,2},{
3,4} };
int arr3[][4] = {
{
1,2},{
3,4},{
5,6} };
reflection :
- arr2 How is it stored ?

It's not hard to see. , In the use of
{ }When separating arrays , every last{ }Separation will divide the internal into different groups
2.3 The use of two-dimensional arrays
Two dimensional arrays are also accessed through subscripts .
#include <stdio.h>
int main()
{
int arr[3][4] = {
0 };// Incomplete initialization
int i = 0;// That's ok
int j = 0;// Column
// Assign a value to a two-dimensional array
for (i=0; i<3; ++i)
{
for (j=0; j<4; ++j)
{
// Assign a value to a two-dimensional array
sacnf("%d", &arr[i][j]);
}
}
for (i=0; i<3; ++i)
{
for (j=0; j<4; ++j)
{
printf("%d ", arr[i][j]);
}
printf("\n");// Line break
}
return 0;
}
3. Array storage in memory
3.1 One dimensional array storage in memory
Look at the following code
#include <stdio.h>
int main()
{
int arr[10] = {
0 };
int i = 0;
for (i = 0; i < 10; ++i)
{
arr[i] = i;
}
return 0;
}

By debugging , We can see , The type is
intA string of elements , In memory is Continuous storage from low address to high address
3.2 Two dimensional array storage in memory
We probably know the storage of one-dimensional arrays in memory , Can you expand it , How are two-dimensional arrays stored ?
below , Let's take a look through the following code
#include <stdio.h>
int main()
{
int i = 0;
int arr[3][4] = {
0 };
for (i=0; i<3; ++i)
{
int j = 0;
for (j=0; j<4; ++j)
{
arr[i][j] = i+j;
}
}
return 0;
}

Compare the two display modes , You can see , Two dimensional arrays are also stored continuously in memory , Then we can also regard arrays as such an arrangement .
that , How can we determine how many elements are placed in a row ?
This problem , That explains why when defining a two-dimensional array , The reason why the following figures cannot be missing
4. Array access beyond bounds
Array of Subscript Of Range The rule is 0 ~ n-1 ,C The language itself does not check the bounds of array subscripts , The compiler also doesn't report errors , But that doesn't mean it's right ,
As a programmer , It's best to check the number crossing by yourself
5. Arrays as arguments to functions
Often when we write code , The array will be used as part of the function parameters ,
for example : Using functions to achieve a bubble sort ( Sort an integer array )
#include <stdio.h>
void bubble_sort(int arr[], int sz)
{
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[] = {
1,1,4,5,1,4,1,9,1,9,8,1,0 };
int sz = sizeof(arr)/sizeof(arr[0]);// Determine the elements of the array
// Sort
bubble_sort(arr, sz);
// Print
for (i=0; i<sz; ++i)
{
printf("%d ", arr[i]);
}
return 0;
}

reflection
- If you will
bubble_sortIs it OK to change the function to the following ?
void bubble_sort(int arr[])
{
int sz = sizeof(arr)/sizeof(arr[0]);
// Ditto below
}

You can see it , If the number of elements of the function is required , It doesn't work inside a function .
The main reason is ,** Array parameters ** The message is The address of the first element of the array , What the first element accesses is actually the first digit of the array .
#FF6A6A size = 4> reflection
- If you will
bubble_sortIs it OK to change the function to the following ?
void bubble_sort(int arr[])
{
int sz = sizeof(arr)/sizeof(arr[0]);
// Ditto below
}

You can see it , If the number of elements of the function is required , It doesn't work inside a function .
The main reason is , Array parameters The message is The address of the first element of the array , What the first element accesses is actually the first digit of the array .
边栏推荐
- 2022-07-22 mysql/stonedb并行hashJoin内存占用分析
- Metaltc5.0 realizes webrtc version IPC of Junzheng pure C
- Redis 持久化
- Vs2019 configuration running open3d example
- Don't compare with anyone, just be yourself
- 华为专家自述:如何成为优秀的工程师
- 第一部分—C语言基础篇_11. 综合项目-贪吃蛇
- /etc/rc. Local setting UI program startup and self startup
- [waveform / signal generator] Based on stc1524k32s4 for C on Keil
- UE4/5 无法打开文件“xxx.generated.h”(Cannot open file xxx.generated.h)的解决方法总结
猜你喜欢
![[waveform / signal generator] Based on stc1524k32s4 for C on Keil](/img/62/8bf96bf7617cc9b2682b5a32711878.png)
[waveform / signal generator] Based on stc1524k32s4 for C on Keil

STM32H750VBT6驱动程控增益放大模块PGA113——基于CubeMX的Hal库

Huawei experts' self statement: how to become an excellent engineer

Ue4/5 cannot open the file "xxx.generated.h" (cannot open file xxx.generated.h) solution summary

Practice of online problem feedback module (12): realize image deletion function

Ge port: sgmii mode and SerDes mode

C language to achieve three chess? Gobang? No, it's n-chess

Vs debugging

一个AI玩41个游戏,谷歌最新多游戏决策Transformer综合表现分是DQN的两倍

第一部分—C语言基础篇_11. 综合项目-贪吃蛇
随机推荐
Input some data and find the maximum output. (keyboard and file reading)
Ue4/5 cannot open the file "xxx.generated.h" (cannot open file xxx.generated.h) solution summary
树莓派换源
Redis 持久化
chm文件打开时提示乱码
第一部分—C语言基础篇_11. 综合项目-贪吃蛇
Prediction of advertising investment and sales based on regression analysis -- K neighborhood, decision tree, random forest, linear regression, ridge regression
Day (0~6) represents the starting position of the first day of each month, stop represents the number of days of each month, and there are two blank spaces between each day. Input different days and s
PyTorch 深度学习实践 第10讲/作业(Basic CNN)
华为专家自述:如何成为优秀的工程师
Geek planet ByteDance one stop data governance solution and platform architecture
安装snownlp包过程出现Requirement already satisfied:及Read timed out.问题解决方法
电子商务时代,企业社交电商转型要做什么?
Prompt for garbled code when CHM file is opened
第二部分—C语言提高篇_4. 二级指针
一日一书:机器学习及实践——从零开始通往kaggle竞赛之路
Camera Hal OEM模块 ---- cmr_grab.c
被马斯克热炒的人形机器人Optimus“擎天柱“,中国厂商或后来居上
重磅直播 | ORB-SLAM3系列代码讲解地图点(专题二)
Vs2019 configuration running open3d example

