An array is a convenient way to organize a finite number of variables of the same type in an ordered way. An array is a construct type in which the variables are called elements of the array. The types of array elements can be basic data types, special types and construction types.
A one-dimensional array
A bit array is the simplest array type. It is defined as follows:
Array name of type specifier [constant expression]
The type specifier is the type of each element in the array, and the constant expression is the number of elements in the array
There are two important points to keep in mind when working with one-dimensional arrays
- A constant expression must have a positive integer value
- A reference to an array element starting with subscript 0
Let’s take a look at arrays with a simple example
The code is as follows:
// Public account: C Language Chinese community
#include<stdio.h>
#define N 9
int main(void) {
int arr[N];
int i;
for (i = 0; i < N; i++)
{
arr[i] = i + 1;
printf("arr[%d]=%d\t", i, arr[i]);
if (0 == (i+1) %3)
{
printf("\n"); }}return 0;
}
Copy the code
The running results are as follows:Let’s examine the above code
We define a one-bit array ARR that contains nine elements. We refer to the elements of the array as “array name [subscript]”, treating each element as a common variable. Note that the array arR contains only 9 elements, so the subscript value cannot exceed 8. Otherwise, the subscript error will occur, as shown in the following example:
Be careful when using arrays that are out of bounds, or you may be setting yourself up for a bug.
So what we’re going to do is we’re going to look at a little bit of code and see how arrays are stored in memory, okay
// Public account: C Language Chinese community
#include<stdio.h>
#define N 4
int main(void) {
int arr[N];
int i;
for (i = 0; i < N; i++)
{
arr[i] = i;
printf("&arr[%d]=%d\n", i, &arr[i]);
}
return 0;
}
Copy the code
The running results are as follows:From the result, we can see that each element occupies 4 bytes, and the internal storage structure is shown as follows:
Finally, we’ll reinforce a one-dimensional array with an example
Requirement: Use an array to store the user’s input data and output it in reverse when the input is complete
The code is as follows:
// Public account: C Language Chinese community
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#define N 5
int main(void) {
int arr[N];// Define an array
int i, temp;// Define variables
printf("Please enter an array of 5 elements: \n");
for (i = 0; i < N; i++)
{
scanf("%d", &arr[i]);
}
printf("Read the array as follows: \n");
for (i = 0; i < N; i++)
{
printf("%d ",arr[i]);
}
printf("\n");
for (i = 0; i < 2; i++)// Switch the positions of the elements in the array
{
temp = arr[i];
arr[i] = arr[4 - i];
arr[4 - i] = temp;
}
printf("The output reverse array is as follows: \n");
for (i = 0; i < N; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
Copy the code
The running results are as follows:
2 d array
The general form for a two-dimensional array definition is as follows:
Array name of type specifier [constant expression 1][Constant expression 2]
The only difference from the definition of a one-dimensional array is that there is a constant expression 2, where constant expression 1 is the length of the first dimension and constant expression 2 is the length of the second dimension. When dealing with two-dimensional arrays, the array is treated as a matrix for ease of understanding, with constant expression 1 representing the number of rows and constant expression 2 representing the number of columns of the matrix. As with one-dimensional arrays, constant expressions cannot be variables when defining two-dimensional arrays. Let’s look at the definition of a two-dimensional array.
// Public account: C Language Chinese community
#include<stdio.h>
#define M 4
#define N 3
int main(a) {
int arr[M][N];
for (int i = 0; i < M; i++)
{
for (int j = 0; j < N; j++)
{
printf("&arr[%d][%d]=%d\t", i, j, &arr[i][j]);
}
printf("\n");
}
return 0;
}
Copy the code
Running results:Consider the two-dimensional array ARR as a matrix. The following figure shows where each element of the array is placed in the matrix.The position of each element in the matrix is determined by the two subscripts of the two-dimensional array. We can define the two-dimensional array int arr[4][3] as composed of arr[4] and int[3], and regard ARr [4] as an integer one-dimensional array containing four elements ARr [0], ARr [1], ARr [2] and ARr [3], each of which is of type int[3]. In other words, each element is a one-dimensional array, and each one-dimensional array contains three elements, such as ARR [0] contains arr[0][1], ARr [0][1], arr[0][2].
After knowing the special structure of two-dimensional arrays, the following figure is used to understand the storage structure of two-dimensional arrays in memory.It can be found from the above storage structure diagram of two-dimensional array in memory that all elements in the two-dimensional array are stored in a contiguous piece of memory, and the memory occupied by the element type is the memory occupied by the length of the first dimension and the length of the second dimension. If the storage mode of a two-dimensional array is analyzed in the way of a matrix, then all elements of the first row of the matrix are stored from left to right, and then all elements of the second row are stored in the same way until all array elements are stored.
Here’s an example of a two-dimensional array:
Enter an arbitrary two-dimensional array of 3 rows and 3 columns and find the sum of the diagonal elements
// Public account: C Language Chinese community
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main(a) {
int arr[3] [3];
int i, j, sum = 0;
printf("please input:\n");
for (i = 0; i < 3; i++)
{
for (j = 0; j < 3; j++)
{
scanf("%d", &arr[i][j]); }}for (i = 0; i < 3; i++)
{
for ( j = 0; j < 3; j++)
{
if(i==j) { sum += arr[i][j]; }}}printf("the result is: %d\n", sum);
return 0;
}
Copy the code
The running results are as follows:
A character array
A character array is, as its name implies, an array whose elements are of character type. It is special in that it is an array whose elements are characters. The general form and considerations of the definition are similar to the general array we explained earlier, except that the type specifier is char. Of course, this does not mean that the type specifier can only be char, but it can also be long, int, etc. However, since char is only one byte in size, using long and int to define character arrays is a waste of resources. Therefore, char is generally used to define character arrays. One dimensional character array first through the following code to see the definition of one dimensional character array.
// Public account: C Language Chinese community
#include<stdio.h>
#define SIZE 20
int main(a) {
long arr1[SIZE] = {'h'.'e'.'l'.'l'.'o'.' '.'w'.'o'.'r'.'l'.'d'.'! '};
char arr2[SIZE] = { 'h'.'e'.'l'.'l'.'o'.' '.'w'.'o'.'r'.'l'.'d'.'! ' };
printf("The size of the long array is: %d\n".sizeof(arr1));
printf("Char array size: %d\n".sizeof(arr2));
return 0;
}
Copy the code
Running results:In the above code, we define different types of character arrays to hold the same character. As you can see, the memory size of the long array is 4 times that of the char array. From this point of view, choosing char as the array type avoids wasting memory space. Here is a code to understand the character array initialization characteristics.
// Public account: C Language Chinese community
#include<stdio.h>
#define SIZE 20
int main(a) {
int i;
char arr[SIZE] = { 'h'.'e'.'l'.'l'.'o'.' '.'w'.'o'.'r'.'l'.'d'.'! ' };
for (i = 0; i < SIZE; i++)
{
printf("%c", arr[i]);
}
return 0;
}
Copy the code
Running results:The result is “Hello World! , which has some null characters. Look at the arR array defined in the code above. The array length is 20 and the number of character elements initialized is 12. The number of character elements initialized is less than the array length. The element ‘\0’ in the array can also be used to mark the end of the array when printing, for example:
// Public account: C Language Chinese community
#include<stdio.h>
#define SIZE 20
int main(a) {
int i;
long arr[SIZE] = { 'h'.'e'.'l'.'l'.'o'.' '.'w'.'o'.'r'.'l'.'d'.'! ' };
for (i = 0; arr[i]! ='\ 0'; i++)
{
printf("%c", arr[i]);
}
return 0;
}
Copy the code
Running results:The output does not contain any null characters because the ‘\0’ flag in the character array is cleverly used. It is also possible to initialize a one-dimensional character array as a string constant, for example:
// Public account: C Language Chinese community
#include<stdio.h>
#define SIZE 20
int main(a) {
int i;
char arr[SIZE] = { "hello world!" };
for (i = 0; arr[i] ! ='\ 0'; i++)
{
printf("%c", arr[i]);
}
return 0;
}
Copy the code
Running results:You can define and initialize a one-dimensional character array without specifying its length. Initializations using a character constant list and a string constant are different, for example:
// Public account: C Language Chinese community
#include<stdio.h>
int main(a) {
int i;
char arr1[] = { "hello world!" };
char arr2[] = {'h'.'e'.'l'.'l'.'o'.' '.'w'.'o'.'r'.'l'.'d'.'! '};
printf("The length of the arR1 array initialized with a string constant is: %d\n".sizeof(arr1));
printf("The length of an ARR2 array initialized with a list of character constants is: %d\n".sizeof(arr2));
return 0;
}
Copy the code
Running results:From the running results show that using this two ways to get the array length is not the same, in the character array by string constants in the process of initialization, stored in the memory automatically add a terminator in the back of the string ‘\ 0’, so get a character array length is the length of the string constant plus 1; The length of a character array defined in this way is the number of characters in the character constant list.
An array of instance
Swap the positions of the largest and smallest numbers in an array
The sample code
//
// Created by bog on 2020/22/09
// Switch the position of the largest and smallest number in the array
//
// Public account: C Language Chinese community
#include "stdio.h"
int main(a){
int a[10];
int max, min;
int m, n;
printf("Please enter 10 numbers: \n");
for (int i = 0; i < 10; i++) {
scanf("%d", &a[i]);
}
printf("The 10 numbers entered are: \n");
for (int i = 0; i < 10; i++) {
printf("%4d", a[i]);
}
printf("\n");
max = a[0];
for (int i = 0; i < 10; i++) {
if (a[i] > max) {
max = a[i];
m = i;
}
}
min = a[0];
for (int i = 0; i < 10; i++) {
if (a[i] < min) {
min = a[i];
n = i;
}
}
a[m] = min;
a[n] = max;
printf("After switching the positions of the largest and smallest numbers: \n");
for (int i = 0; i < 10; i++) {
printf("%4d", a[i]); }}Copy the code
The results Program analysis
First find the maximum and minimum values in the array, record their positions, then swap positions, and finally print the swapped array.