C language programming: array
array
define array
- <type> variable name[number of elements];
int grades[100];
double weight[20];
- The number of elements must be an integer
- Before C99: the number of elements must be a literal value determined at compile time
what is an array
An array is a container with the following characteristics:
Containers are a very important concept in modern programming, or modern programming languages should provide some form of containers, or the capabilities of containers provided by languages are important criteria for judging language capabilities.
- where all elements have the same data type
- Once created, cannot change size
- In memory, the elements of an array are arranged in close order
elements of the array
-
Each cell of the array is a variable of the array type
-
The number placed in the array when using the array
[]
is called a subscript or index, and the subscript counts from 0:The array subscript counting from 0 was originally created by the C language, because it can simplify a lot of operations for the C language compiler.
- grades[0]
- grades[99]
- average[5]
valid subscript range
- Neither the compiler nor the runtime environment will check whether the array subscript is out of bounds, whether it is reading or writing to the array unit
- Once the program is running, an out-of-bounds array can cause problems, causing the program to crash
- segmentation fault
- But it may also be lucky that there are no serious consequences
- Therefore, it is necessary for the programmer to ensure that the program only uses valid subscript values: [0, the size of the array - 1]
Array of length 0?
int a[0];
- can exist but is useless
Array operations
Integrated initialization of arrays
int a[] = {2,4,2,31,312,31,31,231,21,31,3}
-
Directly give the initial value of all elements of the array
-
There is no need to give the size of the array, the compiler will automatically judge
-
If the size of the array is given, but the number of subsequent initial values is insufficient, the subsequent elements will be initialized to 0
-
int[10] = { 2, 3}; PRINT: 2 3 0 0 0 0 0 0 0 0 0
-
Positioning during integration initialization
int a[10] = {
[0] = 2, [2] = 3, 6 ,
};
- Use [n] to give the position in the initialization data
- Data without positioning is appended to the previous position
- Values in other positions are zero-padded
- You can also not give the array size, let the compiler
- It is especially suitable for arrays with sparse initial data
the size of the array
sizeof
Will give the size of the content occupied by the array, in bytes- Thus the length of the array can
siezof(a)/sizeof(a[0])
be used to obtain
When an array is used as a function parameter:
- cannot give size of array in []
- Can no longer be used
sizeof
to calculate the number of elements in an array!
Array assignment
int a[] = {2,34,4,14,14,1,};
int b[] = a;
- Array variables themselves cannot be assigned
- To pass all the elements of one array to another array, traversal must be used
for(int i = 0; i<length; i++){
b[i] = a[i];
}
Example: judging prime numbers
Determine whether it is divisible by a known prime number <X
int isPrime(int x, int list[], int lenght);
int main(){
int i = 3, x, count = 1, list[100] = {
2};
scanf("%d", &x);
while(i < x){
if( isPrime(i, list, count) ){
list[count++] = i;
}
i++;
}
if( isPrime(x, list, count) ){
printf("%d是素数", x);
}else{
printf("%d不是素数", x);
}
}
int isPrime(int x, int list[], int lenght){
for(int i = 0; i < lenght; i++){
if(x % list[i] == 0){
return 0;
}
}
return 1;
}
Construct a prime number table
- Construct a table of prime numbers within N
- Let x be 2
- Mark numbers from 2x3x4x up to ax<n as non-prime
- Let x be the next number that is not marked as non-prime, repeat 2; until all books have been tried
int main(){
int n;
scanf("%d", &n);
int list[n];
for(int i = 0; i < n; i++){
list[i] = 1;
}
for(int x = 2; x < n; x++){
if( list[x] ){
for(int i = 2; x*i<n; i++){
list[x*i] = 0;
}
}
}
}