A pointer to a const variable

 

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info:

To do: Display some strings

To show: A pointer to a const variable

 

 

// A pointer to a const variable

#include <iostream>

using namespace std;

 

int main(void)

{

const char *BuffOne = "Testing";

 

cout<<"The data pointed by BuffTwo is "<<BuffOne<<endl;

 

// The const pointer BuffOne assigned to the const pointer ThePtr is OK...

const char *ThePtr = BuffOne;

cout<<"The data pointed by ThePtr is "<<ThePtr<<endl;

// The following code will generate an error, cannot modify the const....

// *ThePtr = 'z';

return 0;

}

 

Output example:

 

The data pointed by BuffTwo is Testing

The data pointed by ThePtr is Testing

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index