指针变量在内存中有自己的地址,并且占四个字节。废话不多说,有代码为证。
#include "stdafx.h"
#include<iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int m =-1;
cout<<"m"<<m<<endl;
int* dwm=&m;
cout<<"&m "<<&m<<endl;
cout<<"&dwm "<<&dwm<<endl;
cout<<"dwm "<<dwm<<endl;
cout<<"*dwm "<<*dwm<<endl;
cout<<"&*dwm "<<&*dwm<<endl;
cout<<" sizeof(dwm)"<<sizeof(dwm)<<endl;
cout<<"sizeof(*dwm)"<<sizeof(*dwm)<<endl;
short int n=1;
short int* dwn=&n;
cout<<" sizeof(dwn)"<<sizeof(dwn)<<endl;
cout<<"sizeof(*dwn)"<<sizeof(*dwn)<<endl;
unsigned int q=m;
cout<<"unsigned q "<<q<<endl;
signed int a=m;
cout<<"signed m "<<m;
char chZIFU=a;
char* dwmchZIFU=&chZIFU;
cout<<"sizeof(dwmchZIFU)"<<sizeof(dwmchZIFU)<<endl;
cout<<"sizeof(*dwmchZIFU)"<<sizeof(*dwmchZIFU)<<endl;
int w;
cin>>w;
return 0;
}
从图中我们可以清楚的看到指针变量dwm和dwn以及double类型的指针变量dwZIFU始终占四个字节。而,char字符占一个字节,int型占4个字节。
结论一:在c++中任何变量在内存当中都有自己的地址,包括用来存储变量地址的指针变量在内存当中都有自己的地址。
结论二:在c++中指针变量永远占4个字节。
结论三:unsigned int只能赋值为正数,signed int可以赋值为负数。如果给 unsigned int型变量赋值为负数,则输出4294967295。