c++指针的赋值与相减运算实例源码,指针与其他类型的数据一样可以进行赋值或者相减运算,请看以下c++指针的运算实例:
#include <iostream>
using namespace std;
int main()
{
int *p=new int;
int *p1=new int;
int *p3=new int;
cout<<”指针p保存的空间地址为:\t”<<p<<endl;
cout<<”指针p1保存的空间地址为:\t”<<p1<<endl;
p=p1;
cout<<”经过赋值后,指针p保存的空间地址为:\t”<<p<<endl;
*p=p3-p1;//指针相减意思是两块内存相隔多少字节
cout<<”经过相减后指针p的值为:”<<*p<<endl;
return 0;
}
c++指针比较运算实例源码:
#include <iostream>
using namespace std;
int main()
{
int *p=new int;
int *p1=new int;
if(p>p1){
cout<<”p指针指向的内存地址大于p1指针指向的内存地址\n”;
}
else{
cout<<”p指针指向的内存地址不大于p1指针指向的内存地址\n”;
}
return 0;
}
本文链接地址: c++指针的赋值与相减比较运算实例源码