当前位置:网站首页>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 .
边栏推荐
- 三级分类/菜单的查询——树形结构
- metaRTC5.0实现君正的纯C的webrtc版IPC
- 项目问题积累
- 9. Use grid technology to draw a Pentagon on the screen.
- [learning notes] what happens when the URL is input into the page presentation?
- You don't have to waste your life on others' standards
- 第一部分—C语言基础篇_11. 综合项目-贪吃蛇
- 一首伟大的赞歌
- sojson jsjiami.com. V6 crawler JS reverse
- 在线问题反馈模块实战(十二):实现图片删除功能
猜你喜欢

OWASP TOP10 penetration test

Vs debugging

One book a day: machine learning and practice -- the road to the kaggle competition from scratch

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

STM32 ADC based on Hal library uses DMA multi-channel sampling and solves the problems encountered

xavier_normal_ 初始化测试

17. 什么情况用ArrayList or LinkedList呢?

PyTorch 深度学习实践 第10讲/作业(Basic CNN)

第二部分—C语言提高篇_4. 二级指针
![[wechat applet] understand conditional rendering, list rendering and wxss template style](/img/97/cb78efcbcfe1a598da87751c482a98.png)
[wechat applet] understand conditional rendering, list rendering and wxss template style
随机推荐
【C语言】操作符详解(深入理解+整理归类)
PyTorch 深度学习实践 第10讲/作业(Basic CNN)
在线问题反馈模块实战(十二):实现图片删除功能
重磅直播 | ORB-SLAM3系列代码讲解地图点(专题二)
Don't care too much about others' eyes, it will erase your glory
渗透学习-SQL注入篇-靶场篇-安全狗的安装与绕过实验(后续还会更新)
Redis 持久化
[USB voltmeter and ammeter] Based on stm32f103c8t6 for Arduino
Redis basic type - ordered set Zset
第二部分—C语言提高篇_3. 指针强化
聚合型新生态模式-分享购,会员及奖励制度
电子商务时代,企业社交电商转型要做什么?
处理树形结构数据
STM32 ADC based on Hal library uses DMA multi-channel sampling and solves the problems encountered
[wechat applet] understand conditional rendering, list rendering and wxss template style
你就是你,没有人可以取代
UE4/5 无法打开文件“xxx.generated.h”(Cannot open file xxx.generated.h)的解决方法总结
Redis fragment cluster
Never lose yourself!
STM32 external interrupt (register version)

