指针的应用及如何来直接操作内存

      计算机与编程 2007-6-26 14:12:00

#include <iostream>
#include <string>

using namespace std;

void main()

{
 int a,b;
 int *p1,*p2;
//  void exchanged (int *t1,int *t2);
 
 a=1,b=2;
 p1=&a,p2=&b;

 cout<<"a="<<a<<" , "<<"b="<<b<<" , "<<endl;
 cout<<"a="<<&a<<" , "<<"&b="<<&b<<" , "<<endl<<"\n";

 cout<<"*p1="<<*p1<<" , "<<"*p2="<<*p2<<" , "<<endl; //pointer 's value
 cout<<"p1="<<p1<<" , "<<"p2="<<p2<<" , "<<endl<<"\n"; //pointer 's point the a&b 's add

 int *exchange;
 exchange=p1,p1=p2,p2=exchange;

 cout<<"all things has exchanged by pointer."<<endl<<"\n";

 cout<<"a="<<a<<" , "<<"b="<<b<<" , "<<endl;
 cout<<"a="<<&a<<" , "<<"&b="<<&b<<" , "<<endl<<"\n";

 cout<<"*p1="<<*p1<<" , "<<"*p2="<<*p2<<" , "<<endl; //pointer 's value
 cout<<"p1="<<p1<<" , "<<"p2="<<p2<<" , "<<endl<<"\n"; //pointer 's point the a&b 's add

 p1+=1;
 cout<<"p1="<<p1<<" , "<<"p2="<<p2<<" , "<<endl; //pointer 's point the a&b 's add
  cout<<"p1="<<p1+1<<" , "<<"p2="<<p2<<" , "<<endl; //pointer 's point the a&b 's add
   cout<<"p1="<<p1+2<<" , "<<"p2="<<p2<<" , "<<endl<<"\n"; //pointer 's point the a&b 's add

 cout<<"*p1="<<*p1<<" , "<<"*p2="<<*p2<<" , "<<endl; //pointer 's value
 cout<<"p1="<<p1<<" , "<<"p2="<<p2<<" , "<<endl<<"\n"; //pointer 's point the a&b 's add

 int * pt = (int*)0x0012FF7C; //operation of RAM address
 *pt = 99;
 cout<<"PT = "<<*pt<<endl;
 cout<<"PT = "<<pt<<endl;

/* float * p = (float*)0x0012febc;
 *p = 0.5;  */

 cout<<"*p1="<<*p1<<" , "<<"*p2="<<*p2<<" , "<<endl; //pointer 's value


// return 0;
}

/*void exchanged (int *t1,int *t2)
{
 int *exchange;
 exchange=t1,t1=t2,t2=exchange;
}*/

标签集:TAGS:
回复Comments() 点击Count()
喜欢就顶一下

回复Comments

{commentauthor}
{commentauthor}
{commenttime}
{commentnum}
{commentcontent}
作者:
{commentrecontent}