Here is a C++ program to illustrate pointer arithmetic
SOURCE CODE OUTPUT
#include<iostream>
using namespace std;
int main()
{
int x=0,*p,*q,y;
p=&x;
q=p;
cout<<"Address in pointer :"<<int(p)<<endl;
++p;
cout<<"After incrementing : "<<int(p)<<endl;
y=p-q;
cout<<"Difference "<<int(p)<<"-"<<int(q)<<" = "<<y<<endl;
++p;
cout<<"Again incrementing :"<<int(p)<<endl;
y=p-q;
cout<<"Now the Difference "<<int(p)<<"-"<<int(q)<<" = "<<y<<endl;
return 0;
}
using namespace std;
int main()
{
int x=0,*p,*q,y;
p=&x;
q=p;
cout<<"Address in pointer :"<<int(p)<<endl;
++p;
cout<<"After incrementing : "<<int(p)<<endl;
y=p-q;
cout<<"Difference "<<int(p)<<"-"<<int(q)<<" = "<<y<<endl;
++p;
cout<<"Again incrementing :"<<int(p)<<endl;
y=p-q;
cout<<"Now the Difference "<<int(p)<<"-"<<int(q)<<" = "<<y<<endl;
return 0;
}
Any questions regarding to program please write in comments.
No comments:
Post a Comment