1

C++指针梳理

 3 years ago
source link: https://www.ascotbe.com/2018/12/07/C++Pointer/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
#include <iostream>
using namespace std;
int main()
{
int a[20] = {1};
int\* b= a ;
cout << b<<endl;//指针类型 int \*,指向的是int a[20],b的值就是int a[20]的地址
cout << &a<<endl; //int a 的地址
cout << &b; //指针的地址,该地址储存这a地址
getchar();
return 0;
}

今天看到了一种写法内容是这样的:

DWORD dw = * (__unaligned DWORD * )pvDataBuffer;

按我的理解就是指向强制转换为指针的指针
所以把 * (__unaligned DWORD * )pvDataBuffer的值赋值给dw

具体简单的可以参考下图

指针的值是指针本身存储的数值,这个值将被编译器当作一个地址,而不是一个一般的数值。在32位程序里,所有类型的指针的值都是一个32位整数,因为32位程序里内存地址全都是32位长。

指针所指向的内存区就是从指针的值所代表的那个内存地址开始,长度为sizeof(指针所指向的类型)的一片内存区。以后,我们说一个指针的值是XX,就相当于说该指针指向了以XX为首地址的一片内存区域;我们说一个指针指向了某块内存区域,就相当于说该指针的值是这块内存区域的首地址。

指针所指向的内存区和指针所指向的类型是两个完全不同的概念。在例一中,指针所指向的类型已经有了,但由于指针还未初始化,所以它所指向的内存区是不存在的,或者说是无意义的。

以后,每遇到一个指针,都应该问问:这个指针的类型是什么?指针指向的类型是什么?该指针指向了哪里?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK