主要就是为了类型安全,虽然指针大小是由操作系统多少位决定的,但指针的增量却是由类型决定的
具体解释如代码所示:
#include <iostream>
using namespace std;
int main() {
int a[2];
double b[2];
int* c = a;
double* d = b;
cout << "intptr size = " << sizeof(c) << ", douptr size = " << sizeof(d) << endl;
cout << "intptr = " << c << ",intpstr + 1 = " << c + 1 << endl;
cout << "douptr = " << d << ",douptr + 1 = " << d + 1 << endl;
return 0;
}
输出:
intptr size = 8, douptr size = 8
intptr = 0x7ffe7e8a4700,intptr + 1 = 0x7ffe7e8a4704
douptr = 0x7ffe7e8a46f0,douptr + 1 = 0x7ffe7e8a46f8
我们可以看到地址的增量是不同的,指针的大小是相同的,我这里是64位系统,32位即位4。
标签:为什么,intptr,int,double,类型,size,douptr,指针 From: https://www.cnblogs.com/hhJoker123/p/16828892.html