Dereferencing Pointers

Pointers must be dereferenced before the data they point to can be used. Dereferencing is accessing the value stored in the assigned memory location. A pointer can be dereferenced by placing an asterisk before it. For example:

*ptr

In this example, the pointer "ptr" is dereferenced. The value stored in the location referenced by "ptr" is now available for use as shown in the following example:

If you were to use the variable "Ptr" in an expression, you would have to dereference it within the expression. An example is displayed below.

x = *ptr + 1;

A dereferenced pointer can be used to store a new value into the location or object at which it is pointing. For example:

ptr = &y;

*Ptr = 7;

In this example, The expression above would result in the variable "y" being given a value of "7", as the above expression sets the value of the pointer "ptr" to "7", and since "ptr" is pointing to "y", this stores "7" into "y", just as would the expression "y = 7;".

If the pointer "ptr" is used in any expression requiring a value other than a pointer value (for example, a numeric value), the result is invalid, as "ptr" is a pointer to a value, not a value itself. For example:

w = ptr + 1

In this example, w is set to invalid, as the pointer "ptr" was not dereferenced. What should have been written is:

w = *ptr + 1

Some VTS functions return pointers. The section that follows discusses these functions.