Catalog:
Definition of arrays
Arrays are ordered sequences of elements. If you name a collection of a finite number of variables of the same type, the name is an array name. Variables that make up an array are called components of an array, elements of an array, and sometimes subscript variables. The number used to distinguish the elements of an array is called the subscript. Array is a form of disorderly organization of elements of the same type in programming for convenience. These disorderly sets of similar data elements are called arrays.
Arrays are collections used to store multiple identical types of data. - [360 Encyclopedia]
1. One-Dimensional Array
1.1 Initialization of the creation of one-dimensional arrays
Array creation: type_t arr_name [const_n]
type_t denotes the type of elements in an array, arr_name refers to the name of the array, followed by const_n, which is a constant expression to specify the size of the array. The following are examples of the creation of the array:
int arr1[10];
float arr2[20];
Generally, when an array is created, it will be initialized accordingly. Only when an initialized array is created, memory will be allocated to it, and the array can be used normally.
There are two kinds of initialization
- Complete initialization
int arr[10] = {0,1,2,3,4,5,6,7,8,9};
- Incomplete initialization
int arr[10] = {1,2,3};//The uninitialized space is initialized to 0 by default.
The Use of 1.2 One-Dimensional Arrays
Arrays are mostly used with subscript reference operators []
#include <stdio.h>
void Init(int arr[],int sz)//Initialize an array by iterating through it using subscript reference operation assignment
{
int i = 0;
for (i=0; i<sz; i++)
{
arr[i] = 0;
}
}
void my_print(int arr[],int sz)//Print array
{
int i = 0;
for (i=0; i<sz; i++)
{
printf("%d ",arr[i]);
}
}
void Reverse(int arr[],int sz)//Inversion of arrays
{
int left = 0;
int right = sz-1;
int tmp = 0;
while (left<right)
{
tmp = arr[left];
arr[left] = arr[right];
arr[right] = tmp;
left++;
right--;
}
}
int main()
{
int arr[10];
int size = sizeof(arr)/sizeof(arr[0]);
int i = 0;
Init(arr,size);
for (i=0; i<size; i++)//Array assignment
{
arr[i] = i;
}
my_print(arr,size);
printf("\n");
Reverse(arr,size);
my_print(arr,size);
printf("\n");
return 0;
}
2. Two-dimensional arrays
2.1 Creation and initialization of two-dimensional arrays
Two-dimensional arrays are similar to one-dimensional arrays. We can think of them as multi-row and multi-column arrays in space.
- Establish
int arr1[3][4];//Three rows and four columns
doble arr2[5][5];
- Initialization
Similarly, the initialization of two-dimensional arrays can be divided into complete initialization and incomplete initialization. Initialization of one-dimensional arrays and creation of one-dimensional arrays at the same time are int arr []= {1,2,3}; the size of arrays can be omitted, and the size of arrays can be determined by the number of elements initialized later. Fixed array form, such as int arr [][3]= {{1,3}, {2,4}; although the number of rows is not given, two rows can be determined according to the subsequent initialization content, so the array is two rows and three columns.
2.2 Use of two-dimensional arrays
The use of two-dimensional arrays is the same as that of one-dimensional arrays, using the following table reference operators through a two-tier for loop
#include <stdio.h>
int main()
{
int arr[10][10] = {0};//Array Creation + incomplete initialization
int i = 0;
int j = 0;
//Incremental assignment by Subscripts
for(i=0; i<10; i++)
{
for(j=0; j<10; j++)
{
arr[i][j] = i*10+j;
}
}
//Loop printing
for(i=0; i<10; i++)
{
for(j=0; j<10; j++)
{
printf("%4d", arr[i][j]);
}
printf("\n");
}
return 0;
}
Pointer access to arrays
3.1 Array Storage in Memory
Storage of one-dimensional arrays in memory
int is 4 bytes, so it's consecutive
Storage of two-dimensional arrays in memory
Like one-dimensional arrays, they are also stored continuously
Pointer access to 3.2 arrays
Because one-dimensional arrays and two-dimensional arrays are stored in the same form in memory, they can be accessed through pointers.
Pointer access to one-dimensional arrays
Pointer access to two-dimensional arrays
Operations on Arrays
There are several operations on arrays
- 1. sizeof (array name) - The array name corresponds to the entire array, and sizeof (array name) calculates the size of the entire array in bytes.
2. & Array name, the address of the array is taken out, except that all the array names are the address of the first element.
3. Arrays pass the address of the first element in the process of function parameterization.
Here is a set of arithmetic problems, which can be very familiar with these knowledge:
int a[] = {1,2,3,4};
printf("%d\n",sizeof(a)); //The whole array size is 16
printf("%d\n",sizeof(a+0));//a[0] address 4
printf("%d\n",sizeof(a+1));//a[1] address 4
printf("%d\n",sizeof(a[1]));//Type Size of Element a[2] (int)4
printf("%d\n",sizeof(&a)); //Remove the address 4 of the array
printf("%d\n",sizeof(*&a));//Remove the address dereference of the entire array and find all elements 16
printf("%d\n",sizeof(&a+1));//Next address 4 for storing array address pointer variables
printf("%d\n",sizeof(&a[0]));//Address 4 of the first element
printf("%d\n",sizeof(&a[0]+1));//Address 4 of the second element
Operational results:
char arr[] = {'a','b','c','d','e','f'};
printf("%d\n", sizeof(arr));//The whole array size is 6
printf("%d\n", sizeof(arr+0));//First element address 4
printf("%d\n", sizeof(*arr));//First element type size 1
printf("%d\n", sizeof(arr[1]));//The second element type size 1
printf("%d\n", sizeof(&arr));//Address 4 of the entire array
printf("%d\n", sizeof(&arr+1));//Next Address 4 in Memory for Array Addresses
printf("%d\n", sizeof(&arr[0]+1));//Address 4 of the second element
printf("%d\n", strlen(arr));//The address of the first element is looked back, and the latter0 unknown is a random value x1
printf("%d\n", strlen(arr+0));//The address of the first element is looked back, and the latter0 unknown is a random value x1
printf("%d\n", strlen(*arr));//* arr is the first element, not the address that does not conform to the strlen parameter
printf("%d\n", strlen(arr[1]));//arr[1] is the second element, not the address that does not conform to the strlen parameter
printf("%d\n", strlen(&arr));//The pointer variable address where the array address is stored (numerically equal to the address of the first element) is looked back, and the latter0 unknown is a random value x1.
printf("%d\n", strlen(&arr+1));//The pointer variable address that holds the array address + 1 is looked back, and the latter0 unknown is a random value x2-6.
printf("%d\n", strlen(&arr[0]+1));//Look back at the address of the second element, and the latter0 unknown is a random value x1-1
Operational results:
char arr[] = "abcdef";//Put the string into the array, and the subsequent 0 will be put into the array together.
printf("%d\n", sizeof(arr));//The size of the entire array is 7
printf("%d\n", sizeof(arr+0));//Header element address 4
printf("%d\n", sizeof(*arr));//Type size of the first element 1
printf("%d\n", sizeof(arr[1]));//Type size of the second element 1
printf("%d\n", sizeof(&arr));//Address 4 of the entire array
printf("%d\n", sizeof(&arr+1));//Next address 4 for storing array address pointer variables
printf("%d\n", sizeof(&arr[0]+1));//Address 4 of the second element
printf("%d\n", strlen(arr));//Number of elements when the address of the first element finds0 later, excluding0 6
printf("%d\n", strlen(arr+0));//Number of elements when the address of the first element finds0 later, excluding0 6
printf("%d\n", strlen(*arr));//First element parameter error
printf("%d\n", strlen(arr[1]));//The second element parameter error
printf("%d\n", strlen(&arr));//Find the pointer variable address that holds the array address backwards (numerically equal to the address of the first element)6
printf("%d\n", strlen(&arr+1));//The pointer variable address where the array address is stored + 1 is looked back, and the unknown value is a random value.
printf("%d\n", strlen(&arr[0]+1));//Look back at the address of the second element and find the number of elements when 0, excluding 0 5.
Operational results:
char *p = "abcdef";
printf("%d\n", sizeof(p));//Address 4 of string
printf("%d\n", sizeof(p+1));//Address 4 of the second element
printf("%d\n", sizeof(*p));//Type size of the first element 1
printf("%d\n", sizeof(p[0]));//Type size of the first element 1
printf("%d\n", sizeof(&p)); //Address 4 for pointer variables to store string addresses
printf("%d\n", sizeof(&p+1));//4
printf("%d\n", sizeof(&p[0]+1));//Address 4 of the second element
printf("%d\n", strlen(p));//Look backwards for the first element. The number of elements when meeting0 is 6.
printf("%d\n", strlen(p+1));//The second element address is looked back, and the number of elements is 5 when encountering0.
printf("%d\n", strlen(*p));//Represents the first element, parameter error
printf("%d\n", strlen(p[0]));//Represents the first element, parameter error
printf("%d\n", strlen(&p));//The pointer variable address where the string address is stored is looked back, and the unknown value is a random value.
printf("%d\n", strlen(&p+1));//The pointer variable address where the string address is stored + 1 is looked back, and the unknown value is a random value.
printf("%d\n", strlen(&p[0]+1));//Look back at the address of the second element and find the number of elements when 0, excluding 0 5.
Operational results:
int a[3][4] = {0};//
printf("%d\n",sizeof(a));//The size of the entire array is 48
printf("%d\n",sizeof(a[0][0]));//Size 4 of the first element in the first row
printf("%d\n",sizeof(a[0]));//Size 16 of the first line
printf("%d\n",sizeof(a[0]+1));//The first line, the address of the second element 4
printf("%d\n",sizeof(a+1)); //Address 4 of the second line
printf("%d\n",sizeof(&a[0]+1));//Address 4 of the second line
printf("%d\n",sizeof(*a)); //Unquote the address of the first line to get the first line element 16
printf("%d\n",sizeof(a[3])); //Type equivalent: a[1] a[2] size 16
Operational results: