Pointer variables

In C we can have variables whose values will be pointers to data of a particular type. Example: a variable whose values will be pointers to doubles.

Declaring pointer variables

The general form is:
     type *nameOfPointer;
     type *nameOfPointer = &aVariable;
Examples:
     int *iPointer;
     double *dPtr;
Note -- the & operator, known as the address of operator is used to get the address of a variable, function, structure etc. It is NOT the reference operator.

It does not matter where you place the * operator i.e. the following are all acceptable.

     double*  ptrDble;
double * ptrDble;
double *ptrDble;

Pointers and non-pointers can be declared together:

     double * ptrDble, aDouble;
where ptrDble is a pointer to a double whereas aDouble is just a variable of type double.

As with all variables, you must initialize a pointer before you actually use it. Otherwise it can lead to unpredictable program behaviour. (i.e. Your program will normally core dump)

double pi, *piPtr; pi = 3.14159; piPtr = π /* This is known as aliasing */

Why do we need to declare the type?

Q: Why is a pointer to an integer a different "type" than a pointer to a double when on a given platform, all low level pointers are represented alike, e.g. as 32 bit unsigned integers.

A: Because C provides operations on pointers (e.g., *, ++, etc) whose meaning depends on the type of thing pointed to.