当前位置:网站首页>C language - array
C language - array
2022-07-07 01:17:00 【Small protrusion ~】
Catalog
1. One dimensional array creation and initialization
1.2 Initialization of an array
edit 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
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 :


notes :C99 The concept of variable length array is introduced in , Allow the size of the array to be specified by variables , If the compiler does not support C99 Variable length array in , Then you can't use .
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 ).
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
about The use of arrays, we introduced an operator before : [ ] , Subscript reference operator . It's actually an array access operator
#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;
}Arrays are accessed using subscripts , The subscript is from 0 Start . The size of the array can be calculated .
1.4 One dimensional array storage in memory
int main
{
int arr[10] = {1,2,3,4,5,6,7,8,9,10};
int sz = sizeof(arr) / sizeof(arr[0]);
int i = 0;
int* p = &arr[0];
for (i = 0; i < sz; i++)
{
printf("%d ", *(p + i));
}
printf("\n");
for (i = 0; i < sz; i++)
{
printf("&arr[%d] = %p <=======> %p\n", i, &arr[i], p + 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
#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 .
int main()
{
int arr[][5] = { {1,2},{3,4},{5,6} };
int i = 0;
for (i = 0; i < sizeof(arr) / sizeof(arr[0]); i++)
{
int j = 0;
for (j = 0; j < sizeof(arr[0]) / sizeof(arr[0][0]); 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
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 .
4.1 Wrong design of bubble sort function
#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 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;
} There is something wrong with this method , 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 ?
Yes 2 Exceptions :
1.sizeof( Array name ), The array name is not the address of the first element of the array , The array name represents the entire array , It calculates the size of the entire array .
2. & Array name , The array name is not the address of the first element of the array , The array name represents the entire array , It takes out the address of 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
#include <stdio.h>
// It's essentially a pointer void bubble_sort(int* arr)
void bubble_sort(int arr[],int sz)
{
//int sz = sizeof(arr) / sizeof(arr[0]);// It doesn't work out sz, Because only one pointer was passed .
int i = 0;
// Number of trips
for (i = 0; i < sz; i++)
{
int flag = 1;// The assumption is ascending
int j = 0;
for (j = 0; j <sz-1-i; j++)
{
// In exchange for
if (arr[j] > arr[j + 1])
{
int tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
flag = 0;
}
if (1 == flag)
{
break;
}
}
}
}
int main()
{
int arr[] = {10,9,8,7,6,5,4,3,2,1};
int sz = sizeof(arr) / sizeof(arr[0]);
bubble_sort(arr,sz);
//int sz = sizeof(arr) / sizeof(arr[0]);
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
return 0;
}边栏推荐
- Informatics Orsay Ibn YBT 1172: find the factorial of n within 10000 | 1.6 14: find the factorial of n within 10000
- 如何管理分布式团队?
- [Niuke] [noip2015] jumping stone
- Part V: STM32 system timer and general timer programming
- Openjudge noi 1.7 08: character substitution
- NEON优化:矩阵转置的指令优化案例
- Eventbus source code analysis
- Neon Optimization: an instruction optimization case of matrix transpose
- Provincial and urban level three coordinate boundary data CSV to JSON
- Js逆向——捅了【马蜂窝】的ob混淆与加速乐
猜你喜欢

ARM裸板调试之JTAG调试体验

Lldp compatible CDP function configuration
![[batch dos-cmd command - summary and summary] - jump, cycle, condition commands (goto, errorlevel, if, for [read, segment, extract string]), CMD command error summary, CMD error](/img/a5/41d4cbc070d421093323dc189a05cf.png)
[batch dos-cmd command - summary and summary] - jump, cycle, condition commands (goto, errorlevel, if, for [read, segment, extract string]), CMD command error summary, CMD error

Let's see through the network i/o model from beginning to end

Dell Notebook Periodic Flash Screen Fault

JTAG principle of arm bare board debugging

Data type of pytorch tensor
![[HFCTF2020]BabyUpload session解析引擎](/img/db/6003129bc16f943ad9868561a2d5dc.png)
[HFCTF2020]BabyUpload session解析引擎

Part VI, STM32 pulse width modulation (PWM) programming

Dell筆記本周期性閃屏故障
随机推荐
[case sharing] basic function configuration of network loop detection
让我们,从头到尾,通透网络I/O模型
Neon Optimization: About Cross access and reverse cross access
NEON优化:性能优化经验总结
【JVM调优实战100例】05——方法区调优实战(下)
tensorflow 1.14指定gpu运行设置
MySQL中回表的代价
MySQL script batch queries all tables containing specified field types in the database
Come on, don't spread it out. Fashion cloud secretly takes you to collect "cloud" wool, and then secretly builds a personal website to be the king of scrolls, hehe
Make a simple graphical interface with Tkinter
Force buckle 1037 Effective boomerang
Address information parsing in one line of code
实现mysql与ES的增量数据同步
Openjudge noi 1.7 08: character substitution
[batch dos-cmd command - summary and summary] - string search, search, and filter commands (find, findstr), and the difference and discrimination between find and findstr
系统休眠文件可以删除吗 系统休眠文件怎么删除
Chenglian premium products has completed the first step to enter the international capital market by taking shares in halber international
A brief history of deep learning (II)
What kind of experience is it to realize real-time collaboration in jupyter
Installation of torch and torch vision in pytorch