Get new post automatically.

Enter your email address:


eliminate confusion

The reason for pointer variables

Eliminating confusion is not an entirely accurate reason for pointer variables. A primary motivation for having a pointer type for each variable type is to help the compiler. Referring back to an earlier example, when we get the contents at an address ("do step 2 on a number"), the compiler must know how to get the contents at the address.
The compiler needs to know two things when getting the contents at an address:
  • How many bytes starting at the given address make up the value. For example, does the address refer to a 2 byte short int or a 4 byte float or an 8 byte double?
  • How to convert those bytes into the value. That is, the process for converting 4 bytes into a float is different than the process for converting 2 bytes into a short int.
So "eliminating confusion" ends up being more of a side-effect of having pointers rather than the reason for having them. That being said, there are programming languages that get by without typing pointers. For example, in the FORTH programming language, an address could just as easily point to a subroutine as point to a floating point number. It's up to the programmer to use the pointer correctly.