C++ PointersExample 1
#include <iostream> using namespace std;
int main() { int num = 5; //the variable num stores an integer, 5 int *where; // where is a pointer, stores an address where = # // where now stores the address of num cout << "The address stored in where = " << where << endl; cout << "The contents of *where = " << *where << endl; return 0; }
&num assigns the address of num to the pointer variable where. The address stored inwhere is machine-dependent.
contents of the address it is applied to. The effect of *where is to return the contentsof address a7b2. Another way to translate *where is to think of it as *(a7b2). Thefirst cout statement prints out the address stored in where, address a7b2. The secondcout statement prints out the contents of *where, which is 5.
part of a variable declaration (int *where) it is seting up a pointer variable. When itis used as part of a statement, (*where) it is dereferencing a memory address.
Assume a is a pointer variable.a = 0;a = NULL;In either case, the pointer variable a is holding no address.
|