C language – array explanation

The concept of array

What is an array? An array is a collection of elements of the same type. We can know from the concept:

?
The array stores one or more data, but the number of array elements cannot be 0.

?
Multiple data stored in the array have the same type.

Arrays are divided into one-dimensional arrays and multi-dimensional arrays. Multi-dimensional arrays are generally two-dimensional arrays.

One-dimensional array

1. Creation and initialization of one-dimensional array

1.1 Creation of Array

The basic syntax for creating a one-dimensional array is as follows:

type_t arr_name [const_n];
//type_t is the element type of the index array
//arr_name is the name of the array
//const_n is a constant expression used to specify the size of the array

Note: The constant value in [ ] is used to specify the size of the array. The size of this array can be specified according to the actual situation.

For example: If we now want to store the English scores of 30 people in a certain class, we can create an array:

int English[30];

Of course we can also create arrays of other types and sizes:

char ch[10];
double score[15];

1.2 Initialization of arrays

Sometimes, when creating an array, we need to give some initial values, which is called initialization.

How to initialize the array? The initialization of an array generally uses brackets and puts the data in curly brackets.

//Fully initialized
2 int arr[5] = {1,2,3,4,5};
3
4 //Incomplete initialization
5 int arr2[6] = {1};//The ?th element is initialized to 1, and the remaining elements are initialized to 0 by default
6
7 //Wrong initialization - too many initialization items
8 int arr3[3] = {1, 2, 3, 4};

1.3 Type of array

Arrays also have types. Arrays are considered a custom type. What remains after removing the array name is the type of the array.

int arr1[10];
int arr2[12];

char ch[5];

The type of the arr1 array is
int[10]

The type of the arr2 array is
int[12]

The type of the ch array is
char[5]

2. Use of one-dimensional arrays

After learning the Basic syntax of one-dimensional arrays, how should we use it?

2.1 Array subscript

C language stipulates that arrays have subscripts, and the subscripts start from 0. Assume that the array has n elements, and the subscript of the last element is n-1. The subscript is equivalent to the number of the array element, as follows:

int arr[10]={1,2,3,4,5,6,7,8,9};

There are several operators provided for accessing arrays in C language.
[]
, this operator is called: subscript reference operator.

With the subscript access operator, we can easily access the elements of the array. For example, if we access the element with subscript 3, we can use arr[3]
, if you want to access the element whose subscript is 6, you can use
arr[6]:

2.2 Printing of array elements

Next, what if you want to access the contents of the entire array? As long as we generate the subscripts of all elements of the array, then we use a for loop to generate subscripts from 0 to 9, and then use subscripts to access them.

#include <stdio.h>
int main()
{
 int arr[10] = {1,2,3,4,5,6,7,8,9,10};
 int i = 0;
 for(i=0; i<10; i + + )
 {
 printf("%d ", arr[i]);
 }
 return 0;
}

The output result is:

3. Storage of one-dimensional arrays in memory

If we want to understand arrays in depth, we’d better understand how arrays are stored in memory.

#include <stdio.h>
int main()
{
 int arr[10] = {1,2,3,4,5,6,7,8,9,10};
 int i = 0;
 for(i=0; i<10; i + + )
 {
 printf(" & amp;arr[%d] = %p\
 ", i, & amp;arr[i]);
 }
 return 0;
}

The output is:

From the output results, we analyze that as the subscript grows, the address of the array changes from small to large, and we find that the difference between every two adjacent elements is 4 (because an integer is 4 bytes). So we conclude: Arrays are stored continuously in memory.

4.sizeof calculates the number of array elements

When traversing an array, we often want to know the number of elements in the array. Is there a way in C language to use a program to calculate the number of array elements? The answer is yes, you can usesizeof.

sizeof
In C language, there is a keyword that can calculate the type or variable size. In fact,
sizeof
You can also calculate arrays

size. For example:

#include <stido.h>
 int main()
 {
   int arr[10] = {0};
   printf("%d\
", sizeof(arr));
   return 0;
 }

The output result here is 40, which is the total size of the memory space occupied by the array. The unit is bytes.

We also know that all elements in the array are of the same type, so as long as we calculate the number of bytes occupied by each element, the number of elements in the array can be calculated. Here we just select the first element and calculate the size.

#include <stido.h>
int main()
{
 int arr[10] = {0};
 printf("%d\
", sizeof(arr[0]));//Calculate the size of each element, the unit is bytes
 return 0;
}

Next, you can calculate the number of elements in the array:

#include <stido.h>
int main()
{
 int arr[10] = {0};
 int sz = sizeof(arr)/sizeof(arr[0]);
 printf("%d\
", sz);
 return 0;
}

The result here is: 10, indicating that the array has 10 elements.

Summarize:

1. The sizeof() operator is used to get the length in bytes. sizeof(array name) is the size of the entire array. 2. sizeof (first element) is to find the size of a single element of the array. The 0 subscript is used because there is at least one valid element in the array, so the 0 subscript will always exist.
3. Arrays are accessed using subscripts, which start from 0.
4. The size of the array can be calculated. It is recommended to use sizeof(arr)/sizeof(arr[0]).

Two-dimensional array

1. Concept of two-dimensional array

The array we learned earlier is called a one-dimensional array. The elements of the array are all built-in types. If we take the one-dimensional array as the element of the array

element, this time it is a two-dimensional array. An array in which a two-dimensional array is used as an array element is called a three-dimensional array. Arrays above a two-dimensional array are collectively called a multidimensional array .

2. Creation of two-dimensional array

So how do we define a two-dimensional array? The syntax is as follows:

type arr_name[constant value 1][constant value 2];
For example:
int arr[3][5];
double data[2][8];

explain:

?
3 means the array has 3 rows

?
5 means there are 5 elements in each row

?
int indicates that each element of the array is an integer type

?
arr is the array name, you can specify the name according to your own needs

3. Initialization of two-dimensional array

Two-dimensional arrays, like one-dimensional arrays, are also initialized using braces.

3.1 Incomplete initialization

int arr1[3][5] = {1,2};
int arr2[3][5] = {0};

3.2 Full initialization

int arr3[3][5] = {1,2,3,4,5, 2,3,4,5,6, 3,4,5,6,7};

3.3 Initialization by row

int arr4[3][5] = {<!-- -->{1,2},{3,4},{5,6}};

3.4 Omit rows during initialization, but columns cannot be omitted

int arr5[][5] = {1,2,3};
int arr6[][5] = {1,2,3,4,5,6,7};
int arr7[][5] = {<!-- -->{1,2}, {3,4}, {5,6}};

4. Use of two-dimensional arrays

4.1 Subscripts of two-dimensional arrays

When we have mastered the creation and initialization of ?-dimensional arrays, how do we use two-dimensional arrays?

In fact, two-dimensional array access also uses the form of subscripts. Two-dimensional arrays have rows and columns. As long as the rows and columns are locked, only one element in the array can be locked. C language stipulates that the rows of a two-dimensional array start from 0, and the columns also start from 0, as shown below:

int arr[3][5] = {1,2,3,4,5, 2,3,4,5,6, 3,4,5,6,7};

The green numbers on the far right side of the picture represent the row numbers, and the blue numbers in the first row represent the column numbers. They all start from 0. For example, if we say: row 2, column 4, we can quickly locate 7.

#include <stdio.h>
int main()
{
 int arr[3][5] = {1,2,3,4,5, 2,3,4,5,6, 3,4,5,6,7};
 printf("%d\
", arr[2][4]);
 return 0;
}

The results are as follows:

4.2 Input and output of two-dimensional array

Through Mion’s study, we know how to access a single element of a two-dimensional array, but how to access the entire two-dimensional array?

In fact, as long as we can generate all row and column numbers according to certain rules; taking the arr array in the above code as an example, the row selection range is 0~2, and the column value range is 0~4, so we All subscripts can be generated with the help of a loop.

#include <stdio.h>
int main()
{
 int arr[3][5] = {1,2,3,4,5, 2,3,4,5,6, 3,4,5,6,7};
 int i = 0;//Traverse?
 //lose?
 for(i=0; i<3; i + + ) //Production number
 {
 int j = 0;
 for(j=0; j<5; j + + ) //Production column number
 {
 scanf("%d", & amp;arr[i][j]); //Input data
 }
 }
 //Output
 for(i=0; i<3; i + + ) //Production number
 {
 int j = 0;
 for(j=0; j<5; j + + ) //Production column number
 {
 printf("%d ", arr[i][j]); //Output data
 }
 printf("\
");
 }
 return 0;
}

The input and output results are:

4.3 Storage of two-dimensional arrays in memory

Like a one-dimensional array, if we want to study how a two-dimensional array is stored in memory, we can also print out the addresses of all elements of the array.

#include <stdio.h>
int main()
{
 int arr[3][5] = { 0 };
 int i = 0;
 int j = 0;
 for (i = 0; i < 3; i + + )
 {
 for (j = 0; j < 5; j + + )
 {
 printf(" & amp;arr[%d][%d] = %p\
", i, j, & amp;arr[i][j]);
 }
 }
 return 0;
}

Output result:

From the results, we can analyze that each element in each row is adjacent, the address difference is 4 bytes, and the two elements at the cross-row position (such as: arr[0][4] and arr[1 ][0]) is also 4 bytes different, so each element in the two-dimensional array is stored continuously.

Note:

  1. The spatial layout of the two-dimensional array in the memory is also linearly continuous and increasing! ! !
  2. A two-dimensional array is essentially a one-dimensional array, but the internal elements are placed in one-dimensional arrays.

The above is the content about C language arrays this time. I hope that students will have a better understanding of arrays after learning.

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge C Skill TreeArrayArray Introduction 195021 people are learning the system