当前位置:网站首页>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 !!!!
边栏推荐
- 【数据聚类】第四章第一节3:DBSCAN性能分析、优缺点和参数选择方法
- Awk getting started to proficient series - awk quick start
- Source code analysis of the implementation mechanism of multisets in guava class library
- Anti clockwise rotation method of event arrangement -- PHP implementation
- Review of week 278 of leetcode II
- Uva536 binary tree reconstruction tree recovery
- Clion configuration of opencv
- [Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 23
- priority_ queue
- Paper notes ACL 2020 improving event detection via open domain trigger knowledge
猜你喜欢
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 21
17. Memory partition and paging
It's hard to hear C language? Why don't you take a look at this (V) pointer
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 19
Fly tutorial 02 advanced functions of elevatedbutton (tutorial includes source code) (tutorial includes source code)
[the way of programmer training] - 2 Perfect number calculation
IPv6 experiment
Exness: positive I win, negative you lose
17.内存分区与分页
When synchronized encounters this thing, there is a big hole, pay attention!
随机推荐
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
IPv6 experiment
01. Basics - MySQL overview
Servlet learning notes
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 21
Show recent errors only command /bin/sh failed with exit code 1
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 11
Map container
BCD code Baidu Encyclopedia
C语言:求100-999是7的倍数的回文数
Introduction to the button control elevatedbutton of the fleet tutorial (the tutorial includes the source code)
How to use the mongodb ID array to get multiple documents- How to get multiple document using array of MongoDb id?
How to use "bottom logic" to see the cards in the world?
03_ Armv8 instruction set introduction load and store instructions
C語言:求100-999是7的倍數的回文數
Abnormal mode of ARM processor
[Yunju entrepreneurial foundation notes] Chapter II entrepreneur test 13
Wechat video Number launches "creator traffic package"
Clockwise rotation method of event arrangement -- PHP implementation
Unity performance optimization reading notes - Introduction (1)