Pointers
A pointer is a variable containing the memory address of another variable.
The following statement declares a pointer
p
containing the address of an
int variable:
int *p;
Given an integer variable:
int i = 3;
the address of variable
i can be assigned to
variable
p:
p = &i;
The & operator returns the address of the variable it precedes.
The content of the variable pointed to by
p
can be retrieved by placing the
* operator in
front of the pointer:
*p;
Example
#include <iostream>
using namespace std;
int main()
{
int *p;
int i = 3;
p = &i;
cout << "The variable pointed to by p contains the value: " << *p;
return 0;
}
Output
The variable pointed to by p contains the value: 3