当前位置:网站首页>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 !!!!
边栏推荐
- 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
- mm_ Cognition of struct structure
- In 2022, financial products are not guaranteed?
- Global and Chinese market for naval vessel maintenance 2022-2028: Research Report on technology, participants, trends, market size and share
- Detailed explanation of NPM installation and caching mechanism
- Realize cross tenant Vnet connection through azure virtual Wan
- Method of setting default items in C # ComboBox control code
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 19
- The latest idea activation cracking tutorial, idea permanent activation code, the strongest in history
- When synchronized encounters this thing, there is a big hole, pay attention!
猜你喜欢

22 API design practices

Ml and NLP are still developing rapidly in 2021. Deepmind scientists recently summarized 15 bright research directions in the past year. Come and see which direction is suitable for your new pit

C语言数组

LVS load balancing cluster deployment - Dr direct routing mode

JD home programmers delete databases and run away. Talk about binlog, the killer of MySQL data backup

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

SAP ui5 date type sap ui. model. type. Analysis of the display format of date

Abnormal mode of ARM processor

DVC use case (VI): Data Registry

How to judge the advantages and disadvantages of low code products in the market?
随机推荐
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 21
Translation D29 (with AC code POJ 27:mode of sequence)
Global and Chinese market for naval vessel maintenance 2022-2028: Research Report on technology, participants, trends, market size and share
Iframe to only show a certain part of the page
Practical dry goods: deploy mini version message queue based on redis6.0
Clockwise rotation method of event arrangement -- PHP implementation
Source code analysis of the implementation mechanism of multisets in guava class library
It's hard to hear C language? Why don't you take a look at this (V) pointer
How to disable debug messages on sockjs stomp - how to disable debug messages on sockjs Stomp
Kivy教程之 08 倒计时App实现timer调用(教程含源码)
MYCAT middleware installation and use
Global and Chinese markets of NOx analyzers 2022-2028: Research Report on technology, participants, trends, market size and share
16.内存使用与分段
【数据聚类】第四章第一节3:DBSCAN性能分析、优缺点和参数选择方法
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 15
How to judge the advantages and disadvantages of low code products in the market?
Introduction to the button control elevatedbutton of the fleet tutorial (the tutorial includes the source code)
《天天数学》连载57:二月二十六日
Cadence physical library lef file syntax learning [continuous update]
vim 出现 Another program may be editing the same file. If this is the case 的解决方法