当前位置:网站首页>C language -- array
C language -- array
2022-07-27 20:18:00 【Lazy Panda】
List of articles
Preface
Hello everyone , Today, let's talk about simple knowledge about arrays .
One 、 What is an array ?
An array is a collection of data of the same type in a continuous space .
int a = 0;// This is an integer variable
When we just need to store 5 Of course, we can create data 5 A variable ,
But if we want to count the whole class 50 The results of three students , Then let's create it again 50 Variables are not so convenient ,
Upward, we should count the whole school 2000 Students' grades ? At this time, just naming is enough for us to have a headache ,
At this time, the simplicity of the array is reflected
The code is as follows ( Example ):
int arr[50] = {
0 };// Be able to store 50 An array of shaped data
int arr[2000] = {
0 };// Deposit 2000 There's a plastic data
1、 Arrays can be created of any type , for example :int,char,double etc.
2、 Arrays can be accessed by subscripts , The subscript operator is :[ ];
What needs to be noted here is The subscript of an array is from 0 At the beginning ,
That is to say, create an array : int arr[5] = { 0 };
arr[0] Is the first element of the array ,arr[1] Is the second element of the array ,
And so on , The last element of the array is arr[4] instead of arr[5]
3、 Array elements are stored in a contiguous memory space
We need to use Fetch address operator :&( Get the first address of the storage space of the variable in memory );
Pictured &arr[0] = 0xE33AF450;(0x Beginning with 16 Base number )
As you know, an integer variable takes up four bytes , So every next element , Its first address will increase 4
Two 、 Exercises :
1. seek 10 The highest score among students
The code is as follows ( Example ):
#include<stdio.h>
int main() {
int arr[10] = {
0 };
puts(" Please enter 10 Scores of students .");//puts(""); Output string and wrap
int i = 0;
for (i = 0; i < 10; i++) {
printf("arr[%d] = ", i);
scanf("%d", &arr[i]);
}
int max = arr[0];// Assign the first student's grades to max
for (i = 1; i < 10; i++) {
// Start with the second student
if (max < arr[i])
max = arr[i];
}
printf(" The highest score is :%d\n", max);
return 0;
}

2. Yes 5 Students' grades are sorted in ascending order
The code is as follows ( Example ):
#include<stdio.h>
int main() {
int arr[5] = {
0 };
int i = 0;
puts(" Please enter 5 Scores of students .");
for (i = 0; i < 5; i++) {
printf("arr[%d] = ", i);
scanf("%d", &arr[i]);
}
for (i = 0; i < 4; i++) {
// Bubble sort
for (int j = 0; j < 5 - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int z = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = z;
}
}
}
puts(" Ascending by bubble sorting .");
for (i = 0; i < 5; i++)
printf("arr[%d] = %d\n", i, arr[i]);
printf("\n");
return 0;
}

3、 ... and 、 Particular attention :
1. There are two special cases of the use of array names
The first is sizeof( Array name ), The array name here represents the whole array , The result is the number of bytes occupied by the entire array
The second is & Array name , The array name here represents the address of the whole array , Find the address size of the entire array
In addition to the above two cases , Other array names represent the address of the first element .
2.int arr[] = {0, 1, 2, 3, 4};
The size of the array is omitted during initialization here , The compiler will automatically initialize the array size according to the number of data we initialize ,
Initialize here as arr[5].
It can be seen that the number of arrays can be omitted , But pay attention to the omission of multidimensional arrays
for example :int arr[3][4] = { 0 }; This is an array with three rows and four columns , We can think of it as the element type int [4], The number of elements is 3 Array of
Here, the number of elements can also be omitted and written as int arr[ ][4] = { 1,2,3,4,5,6 }; At this time, the number of elements is 2
In the same way int arr[ ][4][5] = { 1,2,3,4,5,6 }; Element type is int [4][5] , The number of elements is 1
But attention should not be written int arr[3][] = { 1,2,3,4,5,6 }; Writing in this way means that the element type is ambiguous , The compiler doesn't know how to allocate these integers .
3. When an array is passed as a parameter, the first address of the element is passed , Not the whole array
So if you use sizeof(arr)/ sizeof(arr[0]) In this way, the number of elements cannot be calculated ,
When an array is passed as a parameter, it is impossible to pass the entire array , So if you need to count the number of elements and send them out when passing parameters .
summary
The first part of today is about the most basic knowledge of arrays , The third part is more difficult , We will discuss in detail in the next blog ,
That's it today , Thank you. .
边栏推荐
- Cfssl of pki/tls tool -- the road to dream
- ECU software and hardware architecture
- C193: scoring system
- JS 数组方法 forEach 和 map 比较
- 使用cpolar建立一个商业网站(5)
- 2019年中国智能机市场:华为拿下近4成份额,稳坐国内第一
- Simple application of multipoint bidirectional republication and routing strategy
- 华为全联接大会2022开启曼谷之旅;Facebook推视频收入分成功能,创作者可获20%分成…
- 西数移动硬盘无法读取(高枕无忧的成语)
- Source code analysis of Chang'an chain data storage
猜你喜欢

Solve the problem of displaying the scroll bar when there is no data in the viewui table

C background GC cause and effect

To share the denoising methods and skills of redshift renderer, you must have a look

How to run kevinchappell / FormBuilder

Unified Modeling Language (UML) specification

Can software testing be learned in 2022? Don't learn, software testing positions are saturated

我也是醉了,Eureka 延迟注册还有这个坑

使用cpolar建立一个商业网站(5)

预处理与宏定义

ECU software and hardware architecture
随机推荐
[C #] positive sequence, reverse sequence, maximum value, minimum value and average value
发布2年后涨价100美元,Meta Quest 2的逆生长
[pytorch series] detailed explanation of the torchvision image processing library of pytorch
LG Group announced that it would donate 3million yuan in cash, 1.2 million masks and 10000 sets of protective clothing to Hubei
codeforces每日5题(均1500)-第二十四天
PyQt5快速开发与实战 4.7 QSpinBox(计数器) and 4.8 QSlider(滑动条)
为什么需要第三方支付?
2019年全球半导体营收同比下滑12%,中国市场份额第一
C语言pow函数(c语言中指数函数怎么打)
‘vite‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件
uva1377
什么是多层感知机(什么是多层感知机)
PyQt5快速开发与实战 4.5 按钮类控件 and 4.6 QComboBox(下拉列表框)
C170: retest screening
西数移动硬盘无法读取(高枕无忧的成语)
Datepicker date selector in viewui compatible solution in ie11 browser
Capacitance in series and in parallel and capacitance in series and balance resistance
PMP practice once a day | don't get lost in the exam -7.27 (including agility + multiple choices)
1.2 pedestrian recognition based on incremental generation of occlusion and confrontation suppression (code understanding and experimental progress + Report)
unity2D 动态漫画剧本(给猛虎桥章节做动画演示二)

