You read with a text pointer

You read with a text pointer


When beginners learning pointers, it will be confused for, such as an array of pointers, an array, a pointer to a pointer, etc. to describe the often dizzying, was very iffy

In fact, as long as the pointer understand the nature, knows it exists in the form of memory, then it will not be confused for a variety of pointer

Nature pointer is a variable that refers to the address of an object store

To figure out these problems, you first need to understand the memory

A memory look like?

Our program also variables are stored in memory, so memory is look like it?

Here I use the analogy of comparing the image to illustrate, in a 32bit CPU, it is possible to think of memory as 一栋大楼, each floor 四个房间, each 房间has a 门牌号house number from left to right, in ascending order from bottom to top, as follows shown

Here Insert Picture Description

Corresponding to the above described objects into memory as follows

Building refers to the block of memory

Refers to the room storage unit, a storage unit stores for each byte (8bit)

Refers to the address of the house number of memory cells, each memory cell has its own address, the address difference between two adjacent memory cells 1

For ease of discussion behind, I will back this picture is as follows

Here Insert Picture Description

This layer represents the number of preceding first address storage unit of a (hexadecimal), followed sequentially incremented address storage unit

Second, the variable is stored in memory in the form of

After figuring out memory looks like, we look at the variables exist in the form of memory

First we look at the chartype variable is how storage

We know that charis one byte, it will be in memory, it will occupy a memory cell, as shown in FIG.

Here Insert Picture Description

Char can be seen in the number 0x00000001of memory cells, the address of this variable is0x00000001

Take a look below at intdeformation is how storage

In the CPU of 32bit, inttype variable occupies four bytes, it requires 4 memory cells, which additionally always aligned at the start address stored in 4 bytes, it is stored in the following memory in the form of

Here Insert Picture Description

从图中可以看到,这个变量的起始存储单元编号为0x00000004,所以它的地址为0x00000004

在搞清楚变量在内存中的存储形式后,我们再来躺一躺指针在内存中的存储形式

三、指针在内存中的存储形式

为什么在学习指针在内存中的存储形式前,要先搞清楚变量呢?

因为指针的本质就是一个变量,你无需把它想象地有多么地玄乎,它就是一个变量,只是这个变量存储是其指向对象的地址

我们来看一看char*int*内存中有什么区别?

答案是没有区别,在32bit的CPU中,任何指针都是4个字节大小(占用4个存储单元),不管你是char*int*或者是double*,它们统统是4个字节大小,这也就意味着它们在内存中都是占用4个存储单元,所以它们在内存中的存在形式没有区别,如下图所示

Here Insert Picture Description

上图中,变量中的指都为0,也就意味着指针想在指向空

那么char*int*的究竟有什么区别呢?

答案是编译器对它们的解释不同

编译器对char*变量的解释是,这个指针指向一个char形变量,也就是它所占用的存储单元存放的是一个char形变量的地址

编译器对int*变量的解释是,这个指针指向一个int形变量,也就是它所占用的存储单元存放的是一个int形变量的地址

在搞清楚指针在内存中的存储形式后,我们再来看一看所谓的指向xxx的指针究竟是怎么一回事

3.1 指向普通变量的指针

首先来看一个指向int型变量的指针

int a=1;
int* p = &a;

假设a的地址为0x00000004,p的地址为0x00000008,那么它们在内存中的存在形式如下所示

Here Insert Picture Description

从图中可以看到,变量a的地址为0x00000004,其值为1,指针p的地址为0x00000008,其值为0x00000004,也就意味着它指向了a变量,这也就是所谓的指向int变量的指针在内存中的存在形式

指向char形变量的指针对应如下

char a = 1;
char* p = &a;

假设a的地址为0x00000005,p的地址为0x00000008,那么它们在内存中的存在形式如下所示

Here Insert Picture Description

3.2 指向数组的指针

首先看数组在内存中的形式,数组是一块连续的内存,下面来看一看这个数组在内存中的表现形式

int array[3];

Here Insert Picture Description

执行array表示第一个元素的地址

现在我们定义一个指向数组的指针

int array[3];
int* p = array;

其在内存中的形式如下

Here Insert Picture Description

可以看到,它也仅仅是存储了数组首元素的地址,并没有什么特别

3.3 指针数组

所谓的指针数组是什么呢?

指针数组是一个数组,但是每个元素都是一个指针变量,如下定义

int* parray[3];

其在内存的表现形式如下

Here Insert Picture Description

可以看到,每一个存储单元都可以存放一个int类型变量的地址

3.4 指向指针的指针

所谓指向指针的指针,意思是该指针里面存放的是另一个指针的地址

下面定义中,a是一个int型变量,p1是一个指向int型的指针,其指向a,p2是一个指向int型指针的指针,其指向p1

int a = 1;
int* p1 = &a;
int** p2 = &p1;

它们在内存中的形式如下

Here Insert Picture Description

四、指针取值

在学习了指针在内存中的存储形式之后,你可能会好奇,在int*的指针和char*的指针取值的时候,是如何准确地操作到相应地存储单元呢?毕竟int为4个节点,char为1个节点

先看一下下面这段代码

char a = 1;
char* p = &a;

假设a的地址为0x00000005,p的地址为0x00000008,那么它们在内存中的存在形式如下所示

Here Insert Picture Description

对于以下取值的代码,是如何进行的呢?

*p = 5;

编译器知道p是指向char类型的指针,而char占一个字节,所以在进行*p取值的时候,编译器会解释为,0x00000005存放着一个char型变量,其大小为1字节,所以只会操作到0x00000005处一个存储单元,然后对其按char型变量处理

同理对于int类型,其大小是4个字节,所以对于int型指针取值,会读取其所指地址处4个存储单元,然后按int型变量处理

到这里想必你对指针有了新的理解,本文到此也就结束了。

Published 107 original articles · won 197 Like · views 80000 +

Guess you like

Origin blog.csdn.net/weixin_42462202/article/details/101679015