当前位置:网站首页>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 !!!!
边栏推荐
- Introduction to the button control elevatedbutton of the fleet tutorial (the tutorial includes the source code)
- MYCAT middleware installation and use
- Flet教程之 按钮控件 ElevatedButton入门(教程含源码)
- Force buckle 142 Circular linked list II
- Fastlane one click package / release app - usage record and stepping on pit
- MySQL advanced (Advanced) SQL statement
- [Chongqing Guangdong education] National Open University spring 2019 2727 tax basis reference questions
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 14
- IPv6 experiment
- R语言--readr包读写数据
猜你喜欢
Error: Failed to download metadata for repo ‘AppStream‘: Cannot download repomd. XML solution
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 13
22 API design practices
MySQL performance optimization index
Communication tutorial | overview of the first, second and third generation can bus
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 19
Clion configuration of opencv
昨天的事情想说一下
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 22
《天天数学》连载57:二月二十六日
随机推荐
Globalsign's SSL certificate products
Fastlane 一键打包/发布APP - 使用记录及踩坑
Tableau makes data summary after linking the database, and summary exceptions occasionally occur.
The latest idea activation cracking tutorial, idea permanent activation code, the strongest in history
What if the chat record is gone? How to restore wechat chat records on Apple Mobile
Flet教程之 02 ElevatedButton高级功能(教程含源码)(教程含源码)
Lvs+kept highly available cluster
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
C语言数组
[directory] search
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 5
Paper notes ACL 2020 improving event detection via open domain trigger knowledge
World document to picture
Iframe to only show a certain part of the page
Here, the DDS tutorial you want | first experience of fastdds - source code compilation & Installation & Testing
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 11
Method of setting default items in C # ComboBox control code
17. Memory partition and paging
MySQL advanced review
C语言:围圈报号排序问题