当前位置:网站首页>C language array
C language array
2022-07-04 12:39:00 【Wake up after dark】
Catalog
One dimensional array creation :
One dimensional array initialization :
One dimensional array storage in memory :
The creation of two-dimensional array :
One dimensional array :
One dimensional array creation :
Format : Element type Array name [ Constant expression ]
Such as :int arr[10] ;
const int n=10;
int arr[n]; // from const It's a constant variable In essence, it is a variable , So it doesn't work
notes : Array creation , stay C99 Before standard , [] I want to give one Constant Can only be , You can't use variables . stay C99 The standard supports the concept of variable length arrays .
One dimensional array initialization :
int arr[5]={1,2,3,4} // The result is : 1 2 3 4 0 The number of characters is less than [] From inside 0 To mend
int arr[]={1,2,3,4} // The result is : 1 2 3 4
char ch[]={'a','b','c'} // The result is : a b c
char ch[]={'a',98,'c'} // The result is : a b c Because in ASCII Characters in the table b Of ASCII The value is 98
char ch[4]={'a','b','c'} // The result is : a b c \0
char ch[]="abc" // The result is : a b c \0
int arr[10]={1,2,3,4,5};printf("%d\n",arr[4]);
assignment 1~10
#include <stdio.h>
int main()
{
int arr[10] = {0};
int i = 0;
for(i=0; i<10; i++)
{
arr[i] = i+1;
}
for(i=0; i<10; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
The size of the array can also be calculated :
int arr[10];int sz = sizeof(arr);// Find the number of bytesint sz = sizeof(arr1[10]);// Find the byte of an elementint sz = sizeof(arr)/sizeof(arr[0]);// Find the number of elements
One dimensional array storage in memory :
#include <stdio.h>
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;
}
As the array subscript grows , Address of element , It's also increasing regularly .
Two dimensional array :
The creation of two-dimensional array :
int arr[3][5]; //3 That's ok 5 Column
char arr[3][5]; //3 That's ok 5 Columndouble arr[2][4]; //2 That's ok 4 Column
2D array initialization :
int arr[3][5]={1,2,3,4,5,6};
// { 1 2 3 4 5 }
{ 6 0 0 0 0 }
{ 0 0 0 0 0 }
If you want to put the 1,2 There is the first line ,3,4 There is the second line ,5,6 There is a third line :
int arr[3][5]={ {1,2},{3,4},{5,6}};
// { 1 2 0 0 0 }
{ 3 4 0 0 0 }
{ 5 6 0 0 0 }
When initializing a two-dimensional array Lines can be omitted , Columns cannot be omitted
Want to print this
int arr[3][5]={ {1,2},{3,4},{5,6}};
The code is as follows :
#include <stdio.h>
int main()
{
int arr[3][5] = { {1,2},{3,4},{5,6} };
int i = 0;
for (i = 0; i < 3; i++)
{
int j = 0;
for (j = 0; j < 5; j++)
{
printf("%d", arr[i][j]);
}
printf("\n");
}
return 0;
}
Two dimensional array storage :
#include <stdio.h>
int main()
{
int arr[3][5];
int i = 0;
for(i=0; i<3; i++)
{
int j = 0;
for(j=0; j<5; j++)
{
printf("&arr[%d][%d] = %p\n", i, j,&arr[i][j]);
}
}
return 0; }
Two dimensional arrays are also stored continuously in memory .
Array out of bounds :
#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\n", arr[i]);// When i be equal to 10 When , Cross border visit
}
return 0;
}
Array name :
The array name is the address of the first element of the array .
There are two exceptions :
1.sizeof( Array name ), The array name is not the address of the first element of the array , The array name represents the entire array , It calculates the address of the entire array .
2.& Array name , The array name is not the address of the first element of the array , The array name represents the entire array , It takes out the address of the entire array .
#include <stdio.h>
int main()
{
int arr[10] = {1,2,3,4,5,6,7,8,9,10};printf("%p\n", arr);
printf("%p\n", arr+1);printf("%p\n", &arr[0]);
printf("%p\n", &arr[0]+1);printf("%d\n", sizeof(arr));
printf("%p\n", &arr);
printf("%p\n", &arr+1);return 0;
}
Bubble sort :
Array 2 Compare adjacent elements , If the order is not satisfied, exchange .
Such as : take 3,1,4,2,5,7,6,8,10,9 Sort ( Ascending )
#include <stdio.h>
//void bubble_sort(int* arr[], int sz)
void bubble_sort(int arr[], int sz)
{
int i = 0;
for (i = 0; i < 10; i++)
{
int j = 0;
for (j = 0; j < sz - 1 - i; j++)
{
if (arr[j] > arr[j + 1])
{
int tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
}
}
int main()
{
int arr[10] = { 3,1,4,2,5,7,6,8,10,9 };
int sz = sizeof(arr) / sizeof(arr[0]);
bubble_sort(arr, sz);
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ",arr[i]);
}
return 0;
}
If the sequence is correct , It's just an exchange , You can optimize :
#include <stdio.h>
void bubble_sort(int arr[], int sz)
{
int i = 0;
for (i = 0; i < 10; i++)
{
int flag = 1;
int j = 0;
for (j = 0; j < sz - 1 - i; j++)
{
if (arr[j] > arr[j + 1])
{
flag = 0;
int tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
}
}
if (1 == flag)
{
break;
}
}
}
int main()
{
int arr[10] = { 3,1,4,2,5,7,6,8,10,9 };
int sz = sizeof(arr) / sizeof(arr[0]);
bubble_sort(arr, sz);
int i = 0;
for (i = 0; i < sz; i++)
{
printf("%d ", arr[i]);
}
return 0;
}
The above content is array related .
May all of you encourage it !!!!
边栏推荐
- Error: Failed to download metadata for repo ‘AppStream‘: Cannot download repomd. XML solution
- DC-5靶机
- The solution of permission denied
- Communication tutorial | overview of the first, second and third generation can bus
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 24
- Method of setting default items in C # ComboBox control code
- C语言:求字符串的长度
- MYCAT middleware installation and use
- Global and Chinese market for naval vessel maintenance 2022-2028: Research Report on technology, participants, trends, market size and share
- Global and Chinese markets of NOx analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
Tableau makes data summary after linking the database, and summary exceptions occasionally occur.
Leetcode: 408 sliding window median
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 11
Azure solution: how can third-party tools call azure blob storage to store data?
The database connection code determines whether the account password is correct, but the correct account password always jumps to the failure page with wrong account password
22 API design practices
Communication tutorial | overview of the first, second and third generation can bus
Awk getting started to proficient series - awk quick start
nn. Exploration and experiment of batchnorm2d principle
The frost peel off the purple dragon scale, and the xiariba people will talk about database SQL optimization and the principle of indexing (primary / secondary / clustered / non clustered)
随机推荐
Mongodb vs mysql, which is more efficient
The frost peel off the purple dragon scale, and the xiariba people will talk about database SQL optimization and the principle of indexing (primary / secondary / clustered / non clustered)
Flet教程之 按钮控件 ElevatedButton入门(教程含源码)
17. Memory partition and paging
Kivy tutorial 08 countdown app implements timer call (tutorial includes source code)
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 8
Bottom Logic -- Mind Map
MYCAT middleware installation and use
Review of week 278 of leetcode II
Talk about "in C language"
Practice of retro SOAP Protocol
LVS load balancing cluster deployment - Dr direct routing mode
Unity performance optimization reading notes - explore performance issues -profiler (2.1)
DC-5靶机
Abnormal mode of ARM processor
Global and Chinese market of piston rod 2022-2028: Research Report on technology, participants, trends, market size and share
VBA, JSON interpretation, table structure -json string conversion
Wechat video Number launches "creator traffic package"
How to judge the advantages and disadvantages of low code products in the market?
Googgle guava ImmutableCollections