当前位置:网站首页>Pointer depth solution "three" (array cognition)
Pointer depth solution "three" (array cognition)
2022-07-22 08:48:00 【Populus euphratica under the desert】
Today we mainly talk about the cognition of arrays .
Today, everyone should also be happy .
Catalog
So what happens if you create a variable set in the form of an array ?
Understand the next pointer +1 Size
What if forced conversion happens ?
What if it's a pointer above level 2 ?
Why is the address of the first element the same as that of the whole element ?
Left and right values of array names
First, the array name can be used as the right value , Look at an example :
Can variable names be lvalues ?
Memory allocation in array
Is the memory allocation of the array the same as that of the pointer ? Is it also increasing from low address to high address ?
int a = 10;
int b = 20;
int c = 30;
printf("&a = %p\n", &a);
printf("&b = %p\n", &b);
printf("&c = %p\n", &c);
Every address here is from Decreasing from high to low Of , In essence, it's because a First in stack , First create , And because of The storage structure of the stack area leads to , In this regard, we will talk about stack frame in detail later , Here is a simple diagram for you to understand :( First use the high address of the stack area )
So what happens if you create a variable set in the form of an array ?
Let's say char An example of an array of types :
#define NUM 10
int main()
{
char arr[NUM] = { 0 };
for (int i = 0; i < 10; i++)
{
printf("arr[%d] = %p\n", i, &arr[i]);
}
return 0;
}
We can see that The address of the array is incremented , It is different from the above method of creating variables . Because the space of the array is linearly continuous and increasing , When opening up space, it is to open up space as a whole and release it as a whole , And when opening up , yes The first element is the lowest address .
&a[0] and &a difference
sizeof Use
First, let's add sizeof The rest of the knowledge :
sizeof(a): yes sizeof Put the array name separately in the , Represents the size of the entire array .
sizeof(&a): At this time, the address of the whole array is taken out .
sizeof(&a[0]): What is taken out is the address of the first element of the array .
Understand the next pointer +1 Size
First of all, why do you want to talk about this ? Because we'll be right later Add... To the address of the array 1 Influence .
General pointer
char *a = NULL;
short *b = NULL;
int *c = NULL;
double *d = NULL;
printf("%d\n",a);
printf("%d\n\n",a+1);
printf("%d\n", b);
printf("%d\n\n", b + 1);
printf("%d\n", c);
printf("%d\n\n", c + 1);
printf("%d\n", d);
printf("%d\n\n", d + 1);
This is the result and the relative pointer plus one , This is related to the pointer type . Add one... To the pointer , The essence is to add the size of the type pointed to .( Do not consider forced type conversion ).
What if forced conversion happens ?
If there is a forced conversion , It depends on the type of forced conversion .
What if it's a pointer above level 2 ?
int main()
{
double ******d = NULL;
printf("%d\n", d);
printf("%d\n\n",d + 1);
return 0;
}
Multi level pointer +1 The back will only move backward 4 Bytes , Because the pointer variables of level 2 and above , It's all addresses , The address in 32 All of you are 4 Bytes , So it only moves backward 4 Bytes .
Array elements +1 Result
After talking about the step size of the above pointer , It's better to understand the problem of array step size .
Look at an example :
char a[NUM] = { 0 };
printf("%p\n", &a[0]);
printf("%p\n", &a[0]+1);
printf("%p\n", &a);
printf("%p\n", &a+1);
First we can see , The address of the first element of the array is the same as that of the whole array . First element address +1, It's backward +1 The size of the first element type , The address of the entire array +1, Is to move the entire array size backward . Essentially different types , Because the type of the first element is different from that of the whole array .
Why is the address of the first element the same as that of the whole element ?
The first thing to know is , All address fetching operations , Whether it's plastic surgery , floating-point , Or structure , Including arrays , Are the smallest of many address values . Then the address of the first element of the array is the lowest , Then take the address of the first element , And the address of the whole element , But they are of different types ,+1 The results are different .
Left and right values of array names
First, the array name can be used as the right value , Look at an example :
char arr[NUM] = { 0 };
char *p = arr;
printf("%p\n", p);
printf("%p\n", arr);
return 0;
Here we can see that the variable name is the right value , Is to assign the first element address to the variable p 了 , That's ok ( Out-of-service &arr, Different types )
Can variable names be lvalues ?
int arr[NUM] = { 0 };
arr = { 1, 2, 3, 4, 5 };
j
First , Variables should have space , Arrays are no exception , There is also space , however Array names cannot be lvalues , Because in C In language , Array cannot be assigned as a whole , Can only be initialized as a whole . therefore , Can't assign a value , You can't do lvalue .
Next up :
In the next issue, we'll talk about pointers and arrays ” close “ Relationship
The next issue is more exciting ~!~!~!
边栏推荐
- 一个交易系统需要经过几年的考验才算成功的交易系统,盈利需要几年才算稳定?
- Différence entre les versions Runtime + compilateur et Runtime only
- 量化交易中通过均线系统判断上涨(下跌)动能减弱
- 自学golang【第一章:go语言基础知识】为什么要学习go语言?go语言与c语言的关系?go语言创始人?go语言的特性有哪些?go语言适合做什么?国内外有哪些企业或项目使用go语言?
- Self study golang [3.2go language variable types and conditional statements, constants, variables, enumerations, application of if statements and switch statements]
- 自学golang【3.2go语言变量的类型与条件语句,常量,变量,枚举,if语句与switch语句的应用】
- Self study the definition of golang [3.5go language array, range keyword] array, and use the for loop to traverse one-dimensional array
- Cloud security daily 220721: Cisco hybrid cloud operation and maintenance management solution found the vulnerability of executing arbitrary commands, which needs to be upgraded as soon as possible
- v-7
- 【flex布局快速上手】快速理解flex布局用法,通过常见的四个布局案例解释【详细注释,一看就会】
猜你喜欢
MySQL中MyISAM和InnoDB的区别与实现
Explain the usage of stack, heap and method area in memory
Self study golang [3.4go language functions and pointers] define a function that returns one or more values. For the pointer of go language, the pointer cannot operate. For the parameter transmission
Self study golang [3.1 defining variables] use VaR keyword to define variables, use var() to define variables collectively, omit int keyword and VaR keyword, and use colon: to replace the definition
Self study golang [3.6 slice practice code] slice length, upper limit, copy, delete and increase
Formation and destruction of function stack frames (26 pictures help you understand function stack frames in depth)
Verilog - 74lvc161 counter
MySQL事务
Postman 配置全局变量 postman设置全局token
What is RPA? Recommend automated tools that allow e-commerce operators to operate 10 times more efficiently
随机推荐
[C语言] 文件操作《一》
交易进阶必由之路:从小白到稳定盈利(三)
8-bit complement booth one bit multiplier
How to put the horizontal flashing cursor (_) in notepad++ Change to vertical flashing cursor style (|)?
职业交易者自用多年的终极突破策略分享(附全套交易模板)
How does pytorch convert a variable or tensor to numpy?
Verilog application - 24 second basketball counter
胜率高达93.98%的TPS交易策略
JS时间和时间戳的转换
How does idea import automatically
Self study golang [3.8 use go language to find the longest substring without repeated characters] exercise code
Self study golang [3.3go language loop statement] for loop syntax structure, omit initial conditions, omit incremental conditions, omit the application of end conditions
动态内存管理
自学golang【3.1定义变量】使用var关键字定义变量,使用var()集中定义变量,省略int等关键字以及省略var关键字,用冒号:替代定义
指针总结篇
Partage de la stratégie de percée ultime des commerçants professionnels pendant de nombreuses années (avec un ensemble complet de modèles de transaction)
Soft exam intermediate [Database System Engineer] Chapter 1: computer system knowledge, self-study soft exam notes, preparation for the soft exam in May 2022, computer hardware system CPU composition
Shell exercise: Statistics of word frequency
Collection and map summary
Alibaba cloud technology expert haochendong: cloud observability - problem discovery and positioning practice