当前位置:网站首页>C language -- 22 one dimensional array
C language -- 22 one dimensional array
2022-07-29 08:38:00 【Work hard!】
1、 One dimensional array creation and initialization
1.1 Array creation
An array is a collection of elements of the same type . How to create an array :
type_t arr_name [const_n]
//type_t Is the element type of the exponential group
//condt_n Is a constant expression , Used to specify the size of an array
Example of array creation :
// The basic creation form of array
// Element type Array name Element number
int arr[8];// Array name is smaller arr,[8] It means you want to put 8 Elements , this 8 Elements. What type is each element ? yes int
char ch[5];// Array name ch,[5] It means to put 5 Elements , this 5 The types of the elements are all char type
Error creating : Variables are used in the creation process
c99 Syntax supports variable length arrays ( The size of the array is a variable ), however vs2022 Variable length arrays are not supported, so an error will be reported .
Array creation ,[] You have to give a constant in the , You can't use variables
1.2 Initialization of an array
The initialization of array refers to giving some reasonable initial values to the contents of the array while creating the array ( initialization ). Such as :int a = 10; Is to initialize the array . For initialization of arrays , It is divided into complete initialization and incomplete initialization
- Fully initialized
int main()
{
int a = 10;// initialization
int arr[10] = {
1,2,3,4,5,6,7,8,9,10};// Fully initialized
return 0;
}

- Incomplete initialization
int arr[10] = {
1,2,3,4,5 };// Incomplete initialization
There is one caveat , If in the definition of array , Do not specify the number of elements in the array , Then the array will determine the number of elements according to the number in the subsequent element set . Such as int arr2[] = {1,2,3,4,5};, The number of element sets is 5, So the array size defaults to 5.
Take a look at the following array definitions : If you want to create an array without specifying the size of the array, you have to initialize it . The number of elements in the array is determined according to the contents of initialization . But for the following code, we should distinguish how to allocate memory
char ch1[5] = {
'h','i'};
char ch2[] = {
'h','i' };
// Use string to initialize
char ch3[5] = "hi";// The first three elements of this array will be placed "h"、"i"、"\0", What remains is 0( It can also be understood as \0,, because \0 Of ASCII The code value is also 0)
char ch4[] = "hi";// But this is the space created according to the string content , So there will be no extra , It will only be "h"、"i"、"\0"

About the difference between the two :
a、 The difference between content printing :
char ch2[] = {
'h','i' };// This is put in "h"、"i" Two elements
char ch4[] = "hi";// This will be put in "h"、"i"、"\0" Three elements

char ch4[] = "hi"; It means to give ch4 Opened up a space , Deposit "h"、“i”、“\0" Three elements , But what is in front of these three elements , What is behind these three elements , These we don't know .char ch2[] = { 'h','i' }; It refers to placing in the open space "h”、“i”, At this time, I still don't know what is placed in front of these two elements , I don't know what is placed behind , So when printing the contents of the array, you will find the terminator "\0", End printing whenever you find it .
b、 The difference in length
Add these two sentences of code to verify :
char ch2[] = {
'h','i' };// This is put in "h"、"i" Two elements
char ch4[] = "hi";// This will be put in "h"、"i"、"\0" Three elements
printf("%d\n", strlen(ch2));// Random value
printf("%d\n", strlen(ch4));// The length is 2

2、 The use of one-dimensional arrays
The use of arrays involves an operator [ ]---- Subscript reference operator , It's actually an array access operator .
int main()
{
int arr[10] = {
0 };// Incomplete initialization , The first is 0, The remaining elements are all 0; This initializes all ten elements to 0 It's the same
arr[4] = 5;// Mark the subscript as 4 The element value of is changed to 5
int i = 0;
int sz = sizeof(arr) / sizeof(arr[0]);// Find the length of the array
for (i = 0; i < sz; i++)
{
printf("%d ",arr[i]);
}
return 0;
}
The code runs as follows :
0 0 0 0 5 0 0 0 0 0
summary :
- Arrays are accessed through subscripts , The subscript is from 0 Start
- The size of the array can be calculated
3、 One dimensional array storage in memory
int main()
{
int arr[10] = {
0 };
arr[4] = 5;
int i = 0;
int sz = sizeof(arr) / sizeof(arr[0]);// Find the length of the array
for (i = 0; i < sz; i++)
{
printf("&arr[%d]=%p\n",i, &arr[i]);
//%p It is printed in the format of address --- Hexadecimal printing ( Will make up the number )
}
return 0;
}
&arr[0]=000000CA3CB3F8C8
&arr[1]=000000CA3CB3F8CC
&arr[2]=000000CA3CB3F8D0
&arr[3]=000000CA3CB3F8D4
&arr[4]=000000CA3CB3F8D8
&arr[5]=000000CA3CB3F8DC
&arr[6]=000000CA3CB3F8E0
&arr[7]=000000CA3CB3F8E4
&arr[8]=000000CA3CB3F8E8
&arr[9]=000000CA3CB3F8EC
The difference between each address and each address 4 Bytes , Because every integer element is 4 Bytes , So the difference between two integer elements must be four bytes . From this code and its running results, we can see : One dimensional arrays are stored continuously in memory ; As the array subscript grows , The address changes from low to high ; The array name is the address of the first element of the array
The difference between hexadecimal printing and address printing :
printf("%x\n",0x12);
printf("%p\n",0x12);
12
0000000000000012
Because arrays are stored continuously in memory , Therefore, you can access the address of the first element later, and then get each of the following elements one by one . The code is as follows :
int main()
{
int arr[10] = {
1,2,3,4,5,6,7,8,9,10};
int* p = arr;// The array name is the address of the first element of the array , It's the address , So you can put it in the pointer ;
int i = 0;
for (i = 0; i < 10; i++)
{
printf("%d ",*p);
p++;
}
return 0;
}
The code runs as follows :
1 2 3 4 5 6 7 8 9 10
边栏推荐
- 状态压缩dp
- 【Transformer】SegFormer:Simple and Efficient Design for Semantic Segmentation with Transformers
- C language macro define command exercise
- 7.3-function-templates
- Intel将逐步结束Optane存储业务 未来不再开发新产品
- Chrony 时间同步
- How to quickly experience oneos
- 01背包关于从二维优化到一维
- Use disco diffusion to generate AI artwork in moment pool cloud
- 2022 spsspro certification cup mathematical modeling problem B phase II scheme and post game summary
猜你喜欢

LeetCode力扣题目总结(题目编号:53、3、141、面试题022、剑指offer链表中环的入口节点、20、19、牛客NC1、103、1143、牛客127)

Simple operation of SQL server data table

Tensorboard use

New energy shared charging pile management and operation platform

Application of matrix transpose

信息系统项目管理师必背核心考点(五十三)质量等级

QT learning: use non TS files such as json/xml to realize multilingual internationalization

(视频+图文)机器学习入门系列-第3章 逻辑回归

C language function output I love you

Hal library learning notes - 8 concept of serial communication
随机推荐
MFC integration QT verification and problem handling
7.2-function-overloading
User identity identification and account system practice
Application scheme of charging pile
Clickhouse learning (II) Clickhouse stand-alone installation
To create a thread pool for the rate, start the core thread
Source code compilation pytorch pit
01背包关于从二维优化到一维
Personal study notes
Day5: PHP simple syntax and usage
Background management system platform of new energy charging pile
QT learning: use non TS files such as json/xml to realize multilingual internationalization
Sword finger offer 50. the first character that appears only once
Sudoku (DFS)
[opencv] - Operator (Sobel, canny, Laplacian) learning
Transaction management in SQL Server
Common query optimization technology of data Lake - "deepnova developer community"
Use disco diffusion to generate AI artwork in moment pool cloud
Basic shell operations (Part 2)
Selenium actual combat case crawling JS encrypted data