Game Development Reference
In-Depth Information
cout << "Value of variablePointer: " << variablePointer << endl;
cout << "Value of variablePointer + 1: "
<< variablePointer + 1 << endl;
cout << "Value of memory at dereferenced variablePointer: "
<< *variablePointer << endl;
cout << "Value of memory at dereferenced variablePointer + 1: "
<< *variablePointer + 1 << endl;
return 0;
}
This code creates an
int
variable and assigns it the value of 5. We then create a pointer to the variable.
int variable = 5;
int* variablePointer = &variable;
The * operator after the type name tells the compiler that we are creating a pointer. A pointer is a
variable with a value that is a memory address rather than an actual value. We assign the address of
variable
to
variablePointer
by using the address-of operator
&
.
Figure
4-1
shows the output generated by the code in Listing 4-3.
Figure 4-1.
The output from Listing 4-3
The first line shows the value of
variable
using
cout
as usual.
cout << "Value of variable: " << variable << endl;
The second line prints out the hexadecimal address of the variable using the address-of operator.
cout << "Address of variable: " << &variable << endl;