`
abc20899
  • 浏览: 910732 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

c++指针

 
阅读更多
#include<iostream.h>
using namespace std;

//指针: 存储内存地址的变量
//取得变量的内存地址 在变量名前加&符号
int main(){
	unsigned short shortVar = 15;
	unsigned long longVar = 65535;
	long sVar = -65535;

	cout<<"shortVar's address is:  "<<&shortVar<<endl;
	cout<<"longVar's address is:  "<<&longVar<<endl;
	cout<<"sVar's address is:  "<<&sVar<<endl;


	//将变量的地址存储到指针中,每个变量都有地址,即使不知道变量的地址也可以将变量的地址存到指针中。
//     int *pAge = 0;  //pAge 为int指针  用于存储整型变量的地址。

    //指针的命名 一般以p开头  pAge pNumber     值为0的指针称为空指针。一定要讲指针初始化,没有初始化的指针为失控指针
    //要让一个指针变量存储一个普通变量的地址,则必须将这个普通变量的地址赋给指针变量。
//    int myAge = 40;
//    int *pAgei = 0;
//    pAgei = &myAge; //pAgei 指针存储了  myAge变量的地址
    //简化  int myAge = 40;  int  *pAge = &myAge;


    //指针指向变量的值 赋给 其他变量
//    int yourAge = *pAgei;   //千万不能 int yourAge = pAgei;  这是把内存地址赋给yourAge变量
   // * 在指针中有两种用途
   // 1。 声明指针  int *pNumber = 0;
   // 2。 解除引用  *pNumber = 5;



   // 指针  指针存储的地址  指针存储地址处的值
   // int yourAge = 9;
   // int  *pAgei = &yourAge;



    typedef unsigned short int USHORT;
    USHORT herAge;
    USHORT *pAgeh = 0;
    herAge = 5;

    cout<<"herAge is: "<< herAge <<endl;
    pAgeh = &herAge;
    cout<<"*pAgeh is: "<< *pAgeh <<endl;

    cout<<"set *pAgeh = 7"<<endl;
    *pAgeh = 7;  //相当于herAge 的值设置为  7
    cout<<"herAge is: "<< herAge <<endl;
    cout<<"*pAgeh is: "<< *pAgeh <<endl;

    cout<<"set herAge = 9"<<endl;
    herAge = 9;
    cout<<"*pAgeh is: "<< *pAgeh <<endl;
    cout<<"herAge is: "<< herAge <<endl;

   return 0;
}

/***
shortVar's address is:  0x7fff58d5ac2e
longVar's address is:  0x7fff58d5ac20
sVar's address is:  0x7fff58d5ac18
herAge is: 5
*pAgeh is: 5
set *pAgeh = 7
herAge is: 7
*pAgeh is: 7
set herAge = 9
*pAgeh is: 9
herAge is: 9
/
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics