directory
array: a set of the same set of the same element
one -dimensional array
Statement:
Data type number name name [constant expression];
constant expression: It must be an integer constant greater than zero.
For example
int main ()
{{
char aRR1 [10]; // The type of CHAR contains 10 elements ARR1
int ARR2 [1 + 9]; // The type of an array of 10 elements ARR2
long arr3 [1 * 10]; // The type of type of 10 elements ARR3 contains 10 elements
Float ARR4 [100 /10]; // The type of Float contains 10 elements array ARR4
Double ARR5 [110 % 100]; // The type of Double containing 10 elements array ARR5
Return 0;
}
Note: Create a number of arrays. Before the C99 standard, you must give a constant in [] and cannot use variables. At the C99 standard, the concept of the change of the change is supported.
Initialization:
The initialization of the
array refers to some reasonable initial values (initialization) of the array at the same time as the array.
Complete initialization:
When the number is fully initialized, the constant expression in the array can be omitted. Will automatically determine the size of the array according to the number of elements given
int Main ()
{{
int ARR [] = {1,2,3,4,5,6,7,8,9,10};
int sz = sizeof (arr) /sizeof (arr [0]); // Calculate the number of elements
Printf ("%d", sz);
Return 0;
}
incorrect initialization
When initialized the array, some elements are initialized, and the remaining elements will be automatically supplemented to 0
int Main ()
{{
int ARR [10] = {1,2,3,4,5}; // Apply for 10 integer spaces, only initialization of the first five elements.
int i = 0;
for (i = 0; i <10; i ++)
Printf ("%d", arr [i]);
Return 0;
}
global array:
The initialization of the
global array is somewhat similar to the global variable. When the global array is declared but not initialized, it will be automatically initially zero.
int ARR [10]; // Create a global array
int Main ()
{{
int i = 0;
for (i = 0; i <10; i ++)
Printf ("%d", arr [i]); // All elements of the print array
Return 0;
}
character array
In addition to storing characters in the
CHAR type, you can also store string
Character storage is roughly similar to the character, but there are still some differences
int Main ()
{{
char arr [] = {'h', 'e', 'l', 'l', 'o', '', 'w', 'o', 'r', 'l', 'd'};
char ch [] = "Hello World";
int sz_arr = sizeof (ARR)/sizeof (ARR [0]); // Calculate the number of elements of the ARR array
int sz_ch = sizeof (ch) /sizeof (ARR [0]); // Calculate the number of elements of the CH array
Printf ("ARR array size is => %d \ n", sz_arr);
Printf ("The size of the CH array is => %d \ n", sz_ch);
Return 0;
}
Character arrays are stored-HELLO World-Why is a difference in element?
Open the surveillance and check it, you will find it
Compared to ARR, one more \ 0 is stored in CH.
At the end of the string, the computer will automatically add a \ 0.
Use:
Through the bidding operator [] and elements, the bidding can be used to achieve the function of the function in the array
(It can only reference the array elements one by one, and the whole array cannot be referenced at one time.)
Dispensing: Each element in the array has its own settlement, and the bidding is increased from 0
For example
int Main ()
{{
char arr [] = "abcdefgh";
Printf ("%c \ n", arr [0]); // Print the element with 0 bids 0
printf ("%c \ n", arr [1]); // Print the element with a bid for 1
printf ("%c \ n", arr [2]); // Print the element with 2 bids 2
printf ("%c \ n", arr [3]); // Print the element with 3 bids 3
printf ("%c \ n", arr [4]); // Print the element with 4 bids 4
printf ("%c \ n", arr [5]); // Print the element with 5 bids 5
printf ("%c \ n", arr [6]); // Print the element with 6 bids 6
printf ("%c \ n", arr [7]); // Print the element with 7 bids 7
Return 0;
}
Summary:
The
- array is accessible to use the bidding, and the bidding starts from 0.
The size of the
- array can be calculated.
int arr[10];
int sz = sizeof(arr)/sizeof(arr[0]);
Storage in memory
Array is continuously stored in memory
can be observed by the address of each element in the array
int main()
{
int arr[10] = { 0 };
int i = 0;
int sz = sizeof(arr) / sizeof(arr[0]);
for (i = 0; i < sz; ++i)
printf("&arr[%d] = %p\n", i, &arr[i]);
return 0;
}
Four bytes difference between the address of each element can be found. This array is an integer array, which can be clearly learned.
Array is continuously stored in memory
Note: The name of the array is the first element address
ARR [i] can also be understood as (ARR+I)*
two -dimensional array
Statement
Two -dimensional array statement is somewhat similar to one -dimensional array
Data type number name name [constant expression] [constant rendering];
For example
int ARR1 [3] [4]; The type of type contains 12 (3*4) array ARR1
char aRR2 [3] [5]; The type of the type contains the array of 15 (3*5) elements ARR2
double ARR3 [2] [4]; The type of type containing 8 (2*4) elements ARR3
initialization
Data type number name name [constant expression 1] [constant reaches 2];
expression can be understood asnumber
Exp me can be understood ascolumnnumber
For example
int arr[3][4] = {1,2,3,4};
can be understood as a space for a three -line and four -row, which are like deposit data, and the next line is stored after full.
Note: For the incomplete initialization, the space that is not initialized is zero.
On the premise of this example, a few examples can understand a lot
In the
int arr[3][4] = {
{1,2},{4,5}};
two -dimensional array, the number of rows can be omitted. Will automatically determine the size of the row according to the number of rows that initialize
int arr[][4] = {
{2,3},{4,5}};
Use
The use of
Two -dimensional array also depends on the bidding
Explanation with a function
int main()
{
int row = 0;
int col = 0;
int arr[3][4] = { {1,2,3,4},{5,6,7,8},{9,10,11,12}};
for (row = 0; row < 3; row++)
{
for (col = 0; col < 4; col++)
{
printf("arr[%d][%d]=%2d ", row, col, arr[row][col]);
}
printf("\n");
}
return 0;
}
can passLine biddingandcolumn settingto lock the element to achieve the effect of use
Storage in memory
Two -dimensional array is also continuously stored in memory
Although
#include <stdio.h>
int main()
{
int arr[3][4];
int i = 0;
for (i = 0; i < 3; i++)
{
int j = 0;
for (j = 0; j < 4; j++)
{
printf("&arr[%d][%d] = %p\n", i, j, &arr[i][j]);
}
}
return 0;
}
, when understanding the two -dimensional array, often understands the two -dimensional array as a plane, and the beginning is continuously stored in the memory
As shown in the figure below
Array
When calling the function of the lower bid, you need to pay attention to the scope of the bidding to avoid the situation of crossing the border.
Take one -dimensional array as an example
The
array has N elements, so the scope of the bidding is 0 ~ N-1. When the current is less than 0 or greater than N-1, it belongs to the cross-border visit, which is beyond the interview of the array of legal space.
Two -dimensional arrays the same