当前位置:网站首页>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
边栏推荐
- raid磁盘阵列
- Detailed explanation of LNMP construction process
- DHCP principle and configuration
- Technology sharing | do you know the functions of the server interface automated testing and requests library?
- OSI seven layer model
- RAID磁盘阵列
- Custom component -- data listener
- What is the good brand of air conduction Bluetooth headset and the best brand recommendation of air conduction headset
- Installation and configuration of unit test framework jest with typescript
- Test interview questions collection (II) | test tools (with answers)
猜你喜欢

Technology sharing | send requests using postman

Esxi community network card driver

Qgraphicsview promoted to qchartview

组管理和权限管理

Firewall - iptables firewall (four tables and five links, firewall configuration method, detailed explanation of matching rules)

Cocos2d-x learning notes Tile Map tiledmap

DHCP service

Technology sharing | interface testing value and system

MySQL主从

Vmware workstation configuration net mode
随机推荐
On cookies and session
About gcc:multiple definition of
Tcp/ip five layer model
Custom component -- data listener
Servlet
Ubuntu MySQL setting remote access permissions
[learning notes] coding ability
MOOC翁恺 C语言 第三周:判断与循环:1.判断
Ubuntu18.04 set up redis cluster [learning notes]
1、 PXE overview and installation
[learning notes] drive
Pku-2739-sum of constructive prime numbers
Suger Bi create task
Test interview questions collection (II) | test tools (with answers)
Difference between process and thread
Operation document tree
Centos7 deploy MySQL database server
DHCP service
VSphere esxi 7.0 update 3 release notes
MySQL common commands