#include
#include
using namespace std;
/**
* This C++ program is used to introduce pointer variables.
* The source code is intended to be reviewed in front of
* students wanting to learn about programming languages.
*
* @creator gdt
* @created 02002.11.21
* @version 02016.09.21
*/
int main(int, char**) {
int a, b; // define two regular int variables
int* p; // define a pointer-to-int variable
// at this this point and time, 'a' and 'b' and 'p'
// have unknown values (they contain happy stuff);
// a pointer variable stores the address of a variable,
// but at this spot in the program, 'p' is not "pointing"
// to an int variable yet...
// a pointer variable contains the address of a
// specific type of data; the data type determines
// the amount of memory the pointer is pointing to...
//
// if you are using a 4-byte int system, then a pointer
// to an int variable points to the beginning of 4 bytes
// of memory that contains the value for an int variable...
// print variables to see their initial values...
// [recall] local variables are not initialized
//
cout << "a is " << a << endl
<< "b is " << b << endl
<< "p is " << p << endl << endl;
// print the address of the variables using the
// unary & (address-of) operator...
// [recall] memory is allocated when a variable
// is defined; each memory location has
// an address; a pointer variable stores
// an address value
//
cout << "address of a is " << &a << endl
<< "address of b is " << &b << endl
<< "address of p is " << &p << endl << endl;
a = 2002;
p = &a; // assign the address-of 'a' to 'p'
cout << " a is " << a << endl
<< " p is " << p << endl
<< "*p is " << *p << endl << endl;
// expression *p is using the unary * operator...
// [recall] unary operators take one operand...
// [recall] * is also the binary multiply operator...
// unary * is called the indirection or dereference operator...
// in this example, 'p' is an operand to the * operator...
// the expression *p evaluates to the int value stored
// at the memory location (address) stored in the variable 'p'...
// at this point the program, 'p' is pointing-to the variable 'a'...
// [warn] When reading comments in the source code, you must always
// be sure they match the source code. It's all about The Code.
b = a + 1; // right-hand-side expression is unimportant
p = &b; // assign the address-of variable 'b' to 'p'
// ingoring the use of const, a pointer is a variable;
// therefore, it can point to different variables at
// different times during run-time.
// print to demonstrate the value of 'p' has changed
//
cout << " b is " << b << endl
<< " p is " << p << endl
<< "*p is " << *p << endl << endl;
// Yet-another-program successfully reaches
// the end of the main() function.
return EXIT_SUCCESS;
}
/*** program generated output inserted as a comment block...
a is 134519868
b is 134519500
p is 0x80486b1
address of a is 0xbfffeb64
address of b is 0xbfffeb60
address of p is 0xbfffeb5c
a is 2002
p is 0xbfffeb64
*p is 2002
b is 2003
p is 0xbfffeb60
*p is 2003
***/