The C++Course provides a general introduction to programming in C++. It is based on A.B. Downey's book, How to Think Like a Computer Scientist. Click here for details.


Assigning Pointers and References

As you saw in the syntax of using the "address of" operator, a pointer is assigned to the return value of the "address of" operator. Because the return value of an "address of" operator is a pointer, everything works out and your code should compile. To assign a pointer, it must be given an address in memory as the rvalue, else, the compiler will give you an error.

int x;
int* px = &x;

The above piece of code shows a variable x of type int being declared, and then a pointer px being declared and assigned to the address in memory of x. The pointer px essentially "points" to x by storing its address in memory. Keep in mind that when declaring a pointer, the pointer needs to be of the same type pointer as the variable or constant from which you take the address.

Now here is where you begin to see the differences between pointers and references. To assign a pointer to an address in memory, you had to have used the "address of" operator to return the address in memory of the variable as a pointer. A reference however, does not need to use the "address of" operator to be assigned to an address in memory. To assign an address in memory of a variable to a reference, you just need to use the variable as the rvalue.

int x;
int& rx = x;

The above piece of code shows a variable x of type int being declared, and then a reference rx being declared and assigned to "refer to" x. Notice how the address of x is stored in rx, or "referred to" by rx without the use of any operators, just the variable. You must also follow the same rule as pointers, wherein you must declare the same type reference as the variable or constant to which you refer.

Hypothetically, if you wanted to see what output a pointer would be...

#include <iostream.h>

int main()
{
  int someNumber = 12345;
  int* ptrSomeNumber = &someNumber;

  cout << "someNumber = " << someNumber << endl;
  cout << "ptrSomeNumber = " << ptrSomeNumber << endl;

  return 0;
}

If you compiled and ran the above code, you would have the variable someNumber output 12345 while ptrSomeNumber would output some hexadecimal number (addresses in memory are represented in hex). Now, if you wanted to cout the value pointed to by ptrSomeNumber, you would use this code:

#include <iostream.h>

int main()
{
  int someNumber = 12345;
  int* ptrSomeNumber = &someNumber;

  cout << "someNumber = " << someNumber << endl;
  cout << "ptrSomeNumber points to " << *ptrSomeNumber << endl;

  return 0;
}

So basically, when you want to use, modify, or manipulate the value pointed to by pointer x, you denote the value/variable with *x.

Here is a quick list of things you can do with pointers and references:

  • You can assign pointers to "point to" addresses in memory
  • You can assign references to "refer to" variables or constants
  • You can copy the values of pointers to other pointers
  • You can modify the values stored in the memory pointed to or referred to by pointers and/or references, respectively
  • You can also increment or decrement the addresses stored in pointers
  • You can pass pointers and/or references to functions (Further information on "Passing by reference" can be found HERE)


Last Update: 2005-11-21