Pointers

From NoskeWiki
Jump to navigation Jump to search

About

A pointer is a data type whose value refers directly to (or "points to") another value stored elsewhere in the computer memory using its address. Obtaining the value to which a pointer refers is called dereferencing the pointer.

Learning how to use pointers properly is one of the harder things to learn in programming - and incorrect use of pointers can cause many nightmares.

Examples

EXAMPLE ONE:

#include <iostream>
using namespace std;

int main ()
{
  int firstvalue, secondvalue;
  int * mypointer;

  mypointer = &firstvalue;
  *mypointer = 10;
  mypointer = &secondvalue;
  *mypointer = 20;
  cout << "firstvalue is " << firstvalue << endl;
  cout << "secondvalue is " << secondvalue << endl;
  return 0;
}

OUTPUT:

firstvalue is 10
secondvalue is 20


Links