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
/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值