2007/06/26 | 指向变量的指针变量及指向常量的指针变量...
类别(计算机与编程) | 评论(0) | 阅读(16) | 发表于 17:18

//指向变量的指针变量及指向常量的指针变量...
//2007.06.26.WaNGs.TeAMs...

#include <iostream>
#include <string>

using namespace std;

main ()

{
 int x,y;

 const int z=99;
 int *pointer1,*pointer2;
 const int *pointer3;

 x=10,y=20;
 pointer1=&x,pointer2=&y;
 pointer3=&z;
 cout<<"x="<<x<<", "<<"y="<<y<<", "<<"z="<<z<<", "<<endl;
 cout<<"*pointer1="<<*pointer1<<", "<<"*pointer2="<<*pointer2<<", "<<"*pointer3="<<*pointer3<<", "<<endl;

 cout<<endl<<"p1 and p3 's address has changed."<<endl;
 pointer1=&y; //·2 error C2440: '=' : cannot convert from 'const int *' to 'int *'
 pointer3=&x; //·1
 cout<<"*pointer1="<<*pointer1<<", "<<"*pointer2="<<*pointer2<<", "<<"*pointer3="<<*pointer3<<", "<<endl;

 cout<<endl<<"p1 and p3 will renew."<<endl;
 *pointer1=99; //·3
// *pointer3=99; //·4 the const pointer do not renew a value to a const number.
 cout<<"*pointer1="<<*pointer1<<", "<<"*pointer2="<<*pointer2<<", "<<"*pointer3="<<*pointer3<<", "<<endl;

/*  问题1:你可以把一个INT类型值赋给CONST INT *POINTER3,如://·1
        但是你无法将一个CONST INT类型值赋给INT *POINTER1,如://·2
    问题2:你可以通过一个INT *POINTER给其指向的变量重新赋值,如://·3
        但是你无法通过一个CONST INT *POINTER给其指向的变量重新赋值,如//·4
*/
return 0;

}

0

评论Comments

日志分类
首页[666]
计算机与编程[133]
EMU[40]
UFOs[24]
房产[127]
音乐[13]
LOG[0]
经济[120]
影视[3]
物理[7]
数学[8]
社会[105]
职场[9]
生物医学[18]
生活[59]