当前位置:网站首页>The best way to learn
The best way to learn
2022-04-22 07:34:00 【cxy_ zjt】
Indexes :
1. One dimensional array
2. Two dimensional array
One dimensional array
1. Array creation
stay csdn Typing this kind of code is too monotonous Bloggers upload screenshots


It is not difficult to find through debugging , The other numbers in the array are 0. therefore , We can do this when creating arrays , When I didn't know what to initialize at first , Direct initialization 0, Such as this :int arr[143] = {0}, In the later stage, you can change the use to the corresponding variable , Because if you don't initialize , Just one int arr[143]; So there are random values in the array , If you are uncertain, your code is prone to unpredictable errors , So we should form a good habit
3. Use of arrays :
Let's give you an example :
int arr[5] = {1,2,3,4,5}
But their subscript is 0,1,2,3,4

4. Calculate the size of the array
Sometimes you can calculate the size of an array with strlen It can also be used. sizeof Calculation
Blogger here to elaborate again strlen And sizeof The difference between!
strlen It's actually a function library , It can only measure the length of the string , And only for strings , The purpose is to see if there is... In the string “\0” And then I'll talk about \0 The size of the front is measured
sizeof: The essence is an operator ( Operator ), It is used to calculate the space occupied by variable memory , Any type can be used , Don't pay attention to whether there is \0, Speaking of this , Or some code , Better understanding !

because “zjt” In fact, in the computer, it means { ‘z’,‘j’,‘t’,‘\0’};
Use strlen and sizeof test dy2 The length will be different
Then note that you are using strlen When you need to add a header file <string.h>
I almost forgot !
There is another way to measure the size of the array
int arr[10] = {1,23,4,5};
int sz = sizeof(arr)/sizeof(arr[0]);
So you will ?
Then you can use sizeof This function int arr[10] = {1,2,3,4,5};
Find out what will happen

You must think it's 10 Or 5
It's not
40 = 10*4;
10: Indicates that the space opened up by this array is 10
4: The space occupied by an integer is four bytes
The reason why the last example is so clever is because char The space occupied by the character type itself is a byte
So if not char Type array
int sz = sizeof(arr)/sizeof(arr[0]); We use this
yes char Type words , Can directly
int sz = sizeof(arr):
5. One dimensional array storage
Learn how arrays are stored , Lay the foundation for future operation and use
eg:
Then we use a for Loop and print out the address to see
Explain What you need to print the address is %p;


We can see that the address of the previous string differs by four bytes , The difference between each address of the following string is 1 Bytes , Look ahead , One is int One is char It should be understandable .
And what I want to explain is that arrays are stored continuously in memory , This is very helpful for us to do questions .
Talked about before If :int*p = &a;
So actually p Namely a The address of , also *p = a;
And now we know that arrays are stored continuously in memory
We can also print the address through the dereference operator


We can see As like as two peas It is proved once again that arrays are stored continuously in memory .
2. Two dimensional array
One dimensional array is done , Now we come to the two-dimensional array
Actually, I think it's just as good as math , Two dimensional array , Just like the coordinates of Mathematics
The horizontal and vertical coordinates are the same The two-dimensional array is roughly as follows

Initialization of 2D array
eg:int arr[2][3] = {1,2,3,4,5,6};
Someone wants to ask , The size of an array in a one-dimensional array can be omitted directly , What about two-dimensional arrays ?
Before that, popularizing one was just 【x】【y】; Let's assume that these are the two numbers behind the two-dimensional array that represent the size of the array ,
that x Said line ,y The column , And we can omit rows but not columns when initializing a two-dimensional array
So let's go through Storage of two-dimensional arrays To illustrate , Through the first vs2019 Take a look at the debugging of

We can see that the arrangement of two-dimensional arrays is similar to what we think , But rough storage ??

It is found that the storage of two-dimensional arrays is similar to that of one-dimensional arrays
therefore 
In fact, two-dimensional arrays should be arranged like this , According to the array we set , Two lines in all , Three columns in one row , So I drew it in the picture , If you omit Columns , How should arrays be stored in space ? conversely , If you omit the line , The array only needs to know when to start a new line , Until it is completely stored .
版权声明
本文为[cxy_ zjt]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220612507730.html
边栏推荐
猜你喜欢

C.Ducky Debugging(简单判断/签到)(2021年度训练联盟热身训练赛第五场 )

系统日志文件过大优化

1. Jam packed (Game 5 of 2021 training League warm-up training competition)

Leetcode - 6 - (string multiplication, next larger element < Ⅰ Ⅱ Ⅲ >, K sets of inverted linked list)

F. Find 3-friendly integers (2021 Niuke summer multi school training camp 1)

B. Ball Dropping (简单几何计算 / 相似三角形) (2021牛客暑期多校训练营1)

驱动与R3的通信

Kotlin 协程Flow、StateFlow、ShareFlow

Leetcode - 4 - (longest substring without repeated characters, candy distribution, binary tree traversal)

X64基础(一)
随机推荐
Leetcode - 6 - (string multiplication, next larger element < Ⅰ Ⅱ Ⅲ >, K sets of inverted linked list)
A. Alice and Bob (game? Thinking & Violence) (2021 Niuke summer multi school training camp 1)
The system log file is too large
Leetcode - 1 - (substructure, combination, spiral matrix and full arrangement of tree < Ⅰ Ⅲ >)
Quick sort and merge sort
Codeforces Round #588 (Div. 2) C D
Educational Codeforces Round 125 (Rated for Div. 2)
Kotlin Flow实现线程切换
Codeforces Round #776 (Div. 3)
重写与重载的定义与区别
Change DP (ah ah ah)
L2-002 linked list weight removal (pit of test point 1)
深入理解MySQL(5):详谈MySQL锁算法
Failed to update PIP all the time? Most of them are network problems!
Minimum circle coverage (basis of computational geometry)
Recursive search sequence set
APC(二)
A. Weird Flecks, But OK (计算几何&三维最小圆覆盖)(2021年度训练联盟热身训练赛第一场)
Pointer structure const summary
H.Happy Number (进制转换/第n个特殊数)(2021牛客暑期多校训练营9 )