An array is a collection of elements of the same type.
1. Array creation
int brr[5] = {
1,2,3,4,5 };
char b[5] = "abcd";
int arr[] = {
1,2,4,5,6,7,8,9 };
char a[] = "abcdefg";
In an array, []
a constant must be given in the middle, not a variable . You can also not write, the number of elements is determined by the content of the initialization later.
Note: In the string, there is a terminator at the end ‘\0’
, so the size of the above array is 5.
char []
Two usages of type array:
- It is treated as a normal char array.
- It is used as a string as a whole.
2. The use of one-dimensional arrays
The space of the array is opened up inside the stack frame of the corresponding function, in other words, the space is opened up on the stack (under normal circumstances).
- Arrays are accessed through subscripts, which start from 0.
- The size of the array can be calculated.
int sz = sizeof(arr) / sizeof(arr[0]);
3. Storage of one-dimensional arrays in memory
int main()
{
int arr[] = {
1,2,4,5,6,7,8,9 };
int sz = sizeof(arr) / sizeof(arr[0]);
int i = 0;
for (; i < sz; i++)
{
printf("&arr[%d]=%p\n", i, &arr[i]);
}
return 0;
}
The result is:
as the subscript element increases, the address of the element increases , each time increasing by 4 bytes (int type). Therefore, the array is stored continuously in memory.
When reading, what is read is the starting address, the address of the first byte of the variable.
4. Creation and initialization of a two-dimensional array
int arr[3][4] = {
1,2,3,4,5 };
char brr[3][5] = {
'a','b','c','d' };
Generally, in a two-dimensional array int arr[M][N]
, M can be omitted, but N cannot be omitted .
5. Use of two-dimensional array
Two-dimensional arrays are also used through subscripts.
int main()
{
int arr[3][4] = {
0};
char brr[3][5] = {
'a','b','c','d' };
int i = 0;
for (; i < 3; i++)
{
int j = 0;
for (; j < 4; j++)
{
arr[i][j] = i * 6 + j;
printf("%d\t", arr[i][j]);
}
printf("\n");
}
printf("\n");
return 0;
}
The result is:
6. Storage of two-dimensional array in memory
int main()
{
int arr[3][4] = {
0};
char brr[3][5] = {
'a','b','c','d' };
int i = 0;
for (; i < 3; i++)
{
int j = 0;
for (; j < 4; j++)
{
printf("&arr[%d][%d]=%p\n", i, j, &arr[i][j]);
}
}
return 0;
}
The result is:
Two-dimensional arrays are also stored linearly and continuously.
A two-dimensional array can be regarded as a one-dimensional array in essence, but the array contains several one-dimensional arrays.
Any array is linearly continuous and incrementally stored.
7. Array name
int main()
{
int arr[] = {
1,2,3,4,5 };
printf("%p\n", arr);//首地址
printf("%p\n", &arr);//取出的为数组的地址
printf("%p\n", arr+1);//下一个元素地址
printf("%p\n", &arr+1);//走完整个数组元素,下一个地址
printf("%p\n", arr[1]);//第二个元素的十进制显示
printf("%p\n", &arr[1]);//第二个元素的地址
printf("%p\n", *arr);//首元素的十进制显示
return 0;
}
The array name is generally the address of the first element of the array.
Two exceptions:
- sizeof (array name).
- &Array name, the address of the array is taken out. &Array name, the array represents the entire array.