当前位置:网站首页>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 .
边栏推荐
- 不运动也能增肌???打一针冬眠黑熊的血清就行
- 第二部分—C语言提高篇_4. 二级指针
- 一首伟大的赞歌
- 10分钟就能写出来的——25~30K的国外企业招聘面试考题,这不是轻轻松松吗~
- 电子商务时代,企业社交电商转型要做什么?
- It can be written in 10 minutes -- 25~30k foreign enterprise recruitment interview questions, isn't it easy~
- Libevent and multithreading
- Camera Hal OEM模块 ---- cmr_grab.c
- MySql的DDL和DML和DQL的基本语法
- Cmake notes
猜你喜欢
随机推荐
PyTorch 深度学习实践 第10讲/作业(Basic CNN)
xavier_ normal_ Initialization test
5. Template cache. Drawing a square can only move within the triangle
After grouping, return to the last record group in each group_ Use of concat
Libevent multithreaded server + client source code
【学习笔记】网页出现白屏可能的原因与优化方法
被马斯克热炒的人形机器人Optimus“擎天柱“,中国厂商或后来居上
(笔记整理未完成)【图论:求单源最短路径】
Neo4j modify tag name
avaScript的流程控制语句
【C语言】操作符详解(深入理解+整理归类)
8. Use the quadratic geometry technology to draw a small column on the screen.
先爱自己,再爱别人。
不去和谁比较,只需做好自己
[C language] operator details (in-depth understanding + sorting and classification)
MySql的DDL和DML和DQL的基本语法
Prediction of advertising investment and sales based on regression analysis -- K neighborhood, decision tree, random forest, linear regression, ridge regression
metaRTC5.0实现君正的纯C的webrtc版IPC
SparkSQL核心使用,220724,
Redis 哨兵机制











