//* Demonstration of pointer properties and pointer arithmetic *

#include <iostream> using namespace std; int main() { cout <<"Pointers to variables"<<endl; double a; double * b; // cout<<&a<<"\t"<<b<<"\t"<<a<<"\t"<<*b<<endl; // *b crashes, a & b not initialized warning a=101; b=&a; cout<<&a<<"\t"<<b<<"\t"<<a<<"\t"<<*b<<endl; a=102; cout<<&a<<"\t"<<b<<"\t"<<a<<"\t"<<*b<<endl; *b=103; cout<<&a<<"\t"<<b<<"\t"<<a<<"\t"<<*b<<endl; b=(double*)104; cout<<&a<<"\t"<<b<<"\t"<<a<<endl; // *b crashes cout <<"Pointers to arrays, pointer++ and --"<<endl; double c[3]={201,202,203}; double *d; d=c; cout<<c<<"\t"<<d<<"\t"<<*d<<"\t"<<d[0]<<"\t"<<d[1]<<"\t"<<d[2]<<endl; d++; cout<<c<<"\t"<<d<<"\t"<<*d<<"\t"<<d[0]<<"\t"<<d[1]<<"\t"<<d[2]<<endl; // d[2] might crash // if no other variable owned by this program is placed after it d++; cout<<c<<"\t"<<d<<"\t"<<*d<<"\t"<<d[0]<<"\t"<<d[1]<<"\t"<<d[2]<<endl; // d[1] might crash // too if no other variable owned by this program is placed after it cout <<"Mixing pointer types is bad"<<endl; double e; int * f; e=301; f=(int*)&e; cout<<&e<<"\t"<<f<<"\t"<<e<<"\t"<<*f<<endl; *f=302; f++; *f=303; cout<<&e<<"\t"<<f<<"\t"<<e<<"\t"<<*f<<endl; return(0); }