当前位置:网站首页>9、 C array explanation
9、 C array explanation
2022-07-28 14:54:00 【Rock magnon】
List of articles
One 、 Array basis
1.1 brief introduction
An array is an ordered collection of the same elements of a fixed size
1.2 Array declaration
- Format :type array[array_size]
- give an example :
double array[10]
1.3 Initialize array ( use {} Represents an array )
- At initialization time , The number of elements in braces cannot be greater than the specified number of elements . If you omit the size of the array , The size of the array is the number of elements at initialization
- give an example :
double array[2] = {1.1, 1.2}double array[] = {1.1, 1.2, 1.3}
1.3 Array element assignment
- array[2] = 10 : Assign the third element of the array to 10
1.4 Practice
- Code implementation :
#include<stdio.h> int main(){ int n[10];// Declare the number of elements as 10 The shape array of int i; // Initialize array for(i = 0;i < 10; i++){ n[i] = i + 100; } // The output array for(i = 0;i < 10; i++){ printf(" Element value :%d\n",n[i]); } } - Running results :
Element value :100 Element value :101 Element value :102 Element value :103 Element value :104 Element value :105 Element value :106 Element value :107 Element value :108 Element value :109
Two 、 Multidimensional arrays
2.1 Multidimensional arrays
- Format :
type array_name[size1][size2][size3]...;
2.2 Two dimensional array
Format :
type array_name[ That's ok ][ Column ]Sort format :

Code implementation :
#include<stdio.h> int main(){ int a[3][4] = { { 0,1,2,3}, { 4,5,6,7}, { 8,9,10,11} }; for(int i = 0; i < 3; i++){ for(int j = 0; j < 4; j++){ printf("a[%d][%d]=%d\n",i,j,a[i][j]); } } return 0; }Running results
a[0][0]=0 a[0][1]=1 a[0][2]=2 a[0][3]=3 a[1][0]=4 a[1][1]=5 a[1][2]=6 a[1][3]=7 a[2][0]=8 a[2][1]=9 a[2][2]=10 a[2][3]=11
2.3 A two-dimensional array stores strings , As a one-dimensional array
- Code implementation :
#include<stdio.h> int main(){ // 2 Indicates the number of strings that can be stored // 10 Represents the length of each string char ch[2][10] = { " Zhang Fei "," zhaoyun "}; printf("%s",ch[0]); return 0; } - Running results :
Zhang Fei
3、 ... and 、C Pass an array to a function
3.1 Pass a one-dimensional array as a function parameter
- Three ways of code implementation :
#include<stdio.h> /* The way 1: The formal parameter is a pointer void func(int *param){ } */ void func1(int *array, int m){ printf("func1**********\n"); for(int i = 0; i < m; i++){ printf("%d ",*(array + i)); } printf("\n"); } /* The way 2: A formal argument is an array of defined sizes void func(int array[3]){ } */ void func2(int array[3], int m){ printf("func2**********\n"); for(int i = 0; i < m; i++){ printf("%d ",array[i]); } printf("\n"); } /* The way 3: The formal argument is an array of undefined sizes void func(int param[]){ } */ void func3(int array[], int m){ printf("func3**********\n"); for(int i = 0; i < m; i++){ printf("%d ",array[i]); } printf("\n"); } int main(){ int a[3] = { 0,1,2}; int m = sizeof(a) / sizeof(a[0]); func1(&a[0],3); func2(a,m); func3(a,m); } - Running results :
func1********** 0 1 2 func2********** 0 1 2 func3********** 0 1 2
3.2 Pass a two-dimensional array as a function parameter
- Three implementation methods :
#include<stdio.h> /* The way 1: The formal parameter is a pointer , Using arrays is the property of sequential storage void func(int *param, m, n){ } */ void func1(int *array, int m, int n){ printf("func1**********\n"); for(int i = 0; i < m; i++){ for(int j = 0; j < n; j++){ printf("%d ",*(array + i*n + j));//array Indicates the starting position of the array } printf("\n"); } printf("\n"); } /* The way 2: A formal argument is an array of defined sizes void func(int (*a)[5],int m, int n){ } */ void func2(int (*array)[3], int m, int n){ printf("func2**********\n"); for(int i = 0; i < m; i++){ for(int j = 0; j < n; j++){ printf("%d ",array[i][j]);//array Indicates the starting position of the array } printf("\n"); } printf("\n"); } /* The way 3: The parameters of the first dimension may not be specified , But the parameters of the second dimension must be specified void func(int param[][5], int m, int n){ } */ void func3(int array[][3], int m, int n){ printf("func3**********\n"); for(int i = 0; i < m; i++){ for(int j = 0; j < n; j++){ printf("%d ",array[i][j]);//array Indicates the starting position of the array } printf("\n"); } printf("\n"); } int main(){ int a[3][3] = { { 1,2},{ 4,5,6},{ 7,8,9}}; int m = sizeof(a) / sizeof(a[0]); func1(&a[0][0],3,3); func2(a,3,3); func3(a,3,3); } - Running results :
func1********** 1 2 0 4 5 6 7 8 9 func2********** 1 2 0 4 5 6 7 8 9 func3********** 1 2 0 4 5 6 7 8 9
Four 、sizeof Application of function in array
- In the function , The array name represents an address pointing to the array
- Code implementation :
#include<stdio.h> void func1(int array[]){ // sizeof What is returned is the memory size of an address , Not the memory size of the array printf(" Inside the function sizeof(array) Value (int):%ld\n",sizeof(array)); } void func2(double array[]){ printf(" Inside the function sizeof(array) Value (double):%ld\n",sizeof(array)); } void main(){ int a[] = { 1,2,3,4}; double b[] = { 1,2,3,4,5,6,7,8,9}; int m = sizeof(a); int n = sizeof(a) / sizeof(a[0]); printf(" In the main function sizeof(array) Value :%d\n",m); printf(" Array a Element number :%d\n",n); func1(a); func2(b); } - Running results :
In the main function sizeof(array) Value :16 Array a Element number :4 Inside the function sizeof(array) Value (int):8 Inside the function sizeof(array) Value (double):8
5、 ... and 、 Address of array
5.1 The relationship between array value and array address
a[] = {
1,2,3};
a: Represents the first address of the array , namely a[0] The address of
a[0],a[1]: Represents the value
a,(a+1),(a+2): It represents the address
(a+i): representative a[0+i] The address of
5.2 The difference between array assignment
char a[] = "xiao";// Will add... At the end '\0'
char a[] = {
'x','i','a','o'};// Will not add '\0'
therefore , The former is longer than the latter
边栏推荐
- Added the ability of class @published for @cloudstorage
- Brief introduction and use of mqtt entry level
- Unittest executes runtestcase prompt <_ io. Textiowrapper name= '< stderr>' mode=W encoding=UTF-8 > solution
- Some problems encountered in the development of Excel VBA, solutions, and continuous updates
- ssh服务
- 文件批量重命名工具Bulk Rename Utility
- Redis-配置文件讲解
- 2022 melting welding and thermal cutting examination questions and online simulation examination
- String转为long 类型报错原因:要转为long必须是int、double、float型[通俗易懂]
- SwiftUI 布局 —— 尺寸( 上 )
猜你喜欢

When Xcode writes swiftui code, it is a small trap that compiles successfully but causes the preview to crash
![[thread safety] what risks may multithreading bring?](/img/79/112ab7e586b0bceb296dfddb2728be.png)
[thread safety] what risks may multithreading bring?

@DS('slave') 多数据源兼容事务问题解决方案

Read the introduction tutorial of rainbow

MITK create module
Excel VBA 开发过程中遇到的一些问题,解决方案,持续更新

How to use the C language library function getchar ()

Tdengine helps Siemens' lightweight digital solutions

MQTT入门级简单介绍与使用

C语言实现简单学生成绩管理系统的方法
随机推荐
Brief introduction and use of mqtt entry level
Force deduction solution summary 1331 array sequence number conversion
VTK notes - picker picker summary
基础架构之日志管理平台及钉钉&邮件告警通知
Use of formdata object, VAR formdata=new formdata()
C语言库函数getchar()怎么使用
FormData对象的使用, var formdata=new FormData()
Redis-Redis在Jedis中的使用
Robot mathematics foundation 3D space position representation space position
Crawler: from entry to imprisonment (II) -- Web collector
Core Data 是如何在 SQLite 中保存数据的
Swiftui layout - alignment
Redis-配置文件讲解
[leetcode] sticker spelling (dynamic planning)
Thoughts on the construction of some enterprise data platforms
Hcip day 12
&0xffffffff(0x08)
Hcip day 10
企鹅一面:为什么不建议使用SELECT * ?
Product Manager