How to change the value of a pointer variable C++ source code example
Compiler: Visual C++ Express Edition 2005
Compiled on Platform: Windows 2003 Server Standard Edition
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: none
To do: Changing the pointer variable value in C++ programming
To show: How to change the value of a pointer variable in C++ programming
// a program that changes the value of a pointer variable
#include <iostream>
using namespace std;
void main(void)
{
// declare and initialize two float variables
double var1 = 58.98;
double var2 = 70.44;
// declare a float pointer variable
double *ptr_var;
// make ptr_var point to variable var1
ptr_var = &var1;
// prints 58.98
cout<<"\nThe first value is(var1) "<<*ptr_var;
cout<<"\nThe address of the first data is "<<ptr_var<<"\n";
cout<<"\nThen let the same pointer (*ptr_var)";
cout<<"\npoint to other address...\n";
// make ptr_var point to variable var2
ptr_var = &var2;
// prints 70.44
cout<<"\nThe second value is(var2) "<<*ptr_var;
cout<<"\nThe address of the second data is "<<ptr_var<<endl;
return;
}
Output example:
The first value is(var1) 58.98
The address of the first data is 0012FF5C
Then let the same pointer (*ptr_var)
point to other address...
The second value is(var2) 70.44
The address of the second data is 0012FF4C
Press any key to continue . . .