当前位置:网站首页>Small turtle C (Chapter 6 arrays 1 and 2)
Small turtle C (Chapter 6 arrays 1 and 2)
2022-07-28 07:03:00 【W.934】
The concept of array
Array : A sequence of data of the same type , It's an ordered set .
Each data in the array is called : Array elements , Subscript element
Array elements It is numbered by its location ( Call the subscript of the array element ) To distinguish between .
With array name and subscript All elements in the array can be processed in a unified way , Thus, it is convenient to deal with a batch of data with the same nature .
Be careful : Array element order does not mean element size order .
Definition and reference of one dimensional array
The definition of one-dimensional array : stay C The use of arrays in languages must be defined first .
Type specifier Array name [ Constant expression ];
for example :int a [10] It means that an array of integers is defined , Array name a, This array has 10 Elements ,10 Each element is an integer variable .
It should be noted that :
One 、 A type specifier is any basic or constructive data type . For the same array , All data elements of the same type .
Two 、 The array name is a user-defined array identifier . The writing rules should conform to the writing rules of identifier .
3、 ... and 、 The constant expression in square brackets represents the number of data elements , It also becomes the length of the array .
Four 、 Allow in the same type description , Describes multiple arrays and variables . for example :int a,b,c,d,k1[10],k2[20];
Some common mistakes :
①float a[0]; // The array size is 0 It makes no sense
②int b(2)(3); // You can't use parentheses
③int k,a[k]; // You cannot use variables to describe the number of groups
A reference to one-dimensional array elements
Array elements are the basic elements of an array . Array elements are also variables , Its identification method is array name followed by a subscript . The subscript indicates the order number of the elements in the array .
The general form of array elements is : Array name [ Subscript ] ( Subscripts can be integer constants or integer expressions )
for example :a[0]=a[5]+a[7]-a[2*3] a[i+j] a[i++] Are all legal array elements
Some precautions :
Array elements are often referred to as subscript variables . You have to define the array first , To use subscript variables . stay C Subscript variables can only be used one by one in a language , Instead of referring to the entire array at once .
The following is wrong :printf("%d",a);
When defining an array “ Array name [ Constant expression ]” And references to array elements “ Array name [ Subscript ]” There is a difference .
for example :int a[10]; // Define the array length as 10
t=a[6]; // quote a The sequence number in the array is 6 The elements of . here 6 Does not represent array length .
Initialization of one dimensional array
In addition to using assignment statements to assign values to array elements one by one , Initialization and dynamic assignment can also be used .
Array initialization assignment refers to giving initial values to array elements when defining an array . Array initialization is done in the compile phase . This reduces running time , Increase of efficiency .
Be careful : Previously, you can also specify the initial value of the array number with an assignment statement or an input statement , It's done at run time .
The general form of initialization assignment is :
Type specifier Array name [ Constant expression ]={ value , value ,...... value };
There are several ways to initialize one-dimensional arrays :
( One ) When defining an array, assign initial values to the elements of the array , for example :int a [10]={0,1,2,3,4,5,6,7,8,9};
Put the initial values of the array elements in a pair of flower brackets in turn . After the above definition and initialization ,a[0]=0,a[1]=1,a[2]=2,......,a[8]=8,a[9]=9.
( Two ) You can assign values to just a few elements , for example :int a [10]={0,1,2,3,4};
Definition a Array has 10 Elements , But only 5 An initial value , This means only to the front 5 Each element has its initial value , after 5 Elements are 0.
( 3、 ... and ) If you want all the elements in an array to be 0, It can be written. int a[10]={0,0,0,0,0,0,0,0,0,0}; or int a [10]={0};
( Four ) When assigning initial values to all array elements , Because the number of data has been determined , Therefore, the array length can not be specified .
for example :int a [5]={1,2,3,4,5}; Or you could write it as :int a [ ]={1,2,3,4,5};
In the second way , There are in parentheses 5 Number , The system will define it automatically a The length of the array is 5. But if the array length is different from the number of initial values provided , Array length cannot be omitted . for example , Suppose the length of the array is 10, We can't omit the definition of array length , It must be written as int a [10]={1,2,3,4,5}; Only before initialization 5 Elements , after 5 Elements are 0.
( 5、 ... and ) Dynamic assignment
边栏推荐
- What's a good gift for Tanabata? Niche and advanced product gift recommendation
- What is the good brand of air conduction Bluetooth headset and the best brand recommendation of air conduction headset
- Result fill in the blank (dfs*c language)
- Difference between process and thread
- Software testing (concept)
- [learning notes] coding ability
- Technology sharing | do you know the functions of the server interface automated testing and requests library?
- Wechat applet custom compilation mode
- KVM热迁移
- shell脚本——sort、uniq、tr、数组排序、cut、eval命令配置
猜你喜欢
随机推荐
Applet creation component
Technology sharing | do you know the functions of the server interface automated testing and requests library?
Result number of filled briquettes
Results fill in the blank shopping list (teach you to solve it with Excel)
Servlet
MOOC翁恺C语言 第四周:进一步的判断与循环:1.逻辑类型与运算2.级联和嵌套的判断
DNS正向解析实验
What's a good gift for Tanabata? Niche and advanced product gift recommendation
Test interview questions collection (II) | test tools (with answers)
DHCP原理与配置
Qgraphicsview promoted to qchartview
How to describe a bug and the definition and life cycle of bug level
Hdu-1159-commonsubsequence (LCS longest common subsequence)
组管理和权限管理
Gerapy use
What is a linear table?
Esxi community network card driver
KVM hot migration
My deployment notes
Difference between process and thread









