当前位置:网站首页>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
边栏推荐
- Qt中QTableView设置分页显示的三种方法[通俗易懂]
- @DS('slave') 多数据源兼容事务问题解决方案
- Excel VBA password free view VBE encryption code
- 2、 Declaration and definition of variables and constants
- Hcip day 12
- Hand in hand from 0 to a "Nuggets special attention" Google plug-in, 5000 words detailed vue3 responsive principle, the advantages, disadvantages and choices of several cache read-write schemes, flyin
- Installing redis in Linux
- 基于 MinIO 对象存储保障 Rancher 数据
- How to use the C language library function getchar ()
- VTK annotation class widget vtkborderwidget
猜你喜欢

Hcip day 12

Hcip day 11

Read the introduction tutorial of rainbow

BGP experiment

Tdengine helps Siemens' lightweight digital solutions

Focus on differentiated product design, intelligent technology efficiency improvement and literacy education around new citizen Finance

Digital transformation security issues occur frequently, and Shanshi Netcom helps build a digital government

看了就会的 Rainbond 入门教程

基于 MinIO 对象存储保障 Rancher 数据

十、时间戳
随机推荐
Four basic data types
在 SwiftUI 视图中打开 URL 的若干方法
企鹅一面:为什么不建议使用SELECT * ?
Realization of chat room function
C # 7 methods to obtain the current path
C# 读取ini文件、键值对操作
Interviewer: what are the usage scenarios of ThreadLocal? How to avoid memory leakage?
Various pitfalls encountered in UI development
Third class exercise
Brief introduction of diversity technology
基础架构之日志管理平台及钉钉&邮件告警通知
(function(global,factory){
QT hex, decimal, qbytearray, qstring data conversion
Redis redis use in jedis
How to use the C language library function getchar ()
Why can the anonymous functions of JQ access the methods inside
Redis persistence
Namespace conflict problem
爆肝整理JVM十大模块知识点总结,不信你还不懂
How does core data save data in SQLite