Pointers in C and C++
In C and C , pointers can be used in the same ways as addresses are used in assembly languages. This means they are extremely flexible but must be used with great care. This design offers no solutions to the dangling pointer or lost heap-dynamic variable problems. However, the fact that pointer arithmetic is possible in C and C makes their pointers more interesting than those of the other programming languages.
C and C pointers can point at any variable, regardless of where it is allocated. In fact, they can point anywhere in memory, whether there is a variable
there or not, which is one of the dangers of such pointers.
In C and C , the asterisk (*) denotes the dereferencing operation, and the ampersand (&) denotes the operator for producing the address of a variable. For example, consider the following code:
The assignment to the variable ptr sets it to the address of init. The assignment to count dereferences ptr to produce the value at init, which is then assigned to count. So, the effect of the two assignment statements is to assign the value of init to count. Notice that the declaration of a pointer specifies its domain type.
Notice that the two assignment statements above are equivalent in their effect on count to the single assignment
count = init;
Pointers can be assigned the address value of any variable of the correct domain type, or they can be assigned the constant zero, which is used for nil. Pointer arithmetic is also possible in some restricted forms.
For example, if ptr is a pointer variable that is declared to point at some variable of some data type, then
is a legal expression. The semantics of such an expression is as follows. Instead of simply adding the value of index to ptr, the value of index is first scaled by the size of the memory cell (in memory units) to which ptr is pointing (its base type). For example, if ptr points to a memory cell for a type that is four memory units in size, then index is multiplied by 4, and the result is added to ptr. The primary purpose of this sort of address arithmetic is array manipulation. The following discussion is related to singledimensioned arrays only.
In C and C , all arrays use zero as the lower bound of their subscript ranges, and array names without subscripts always refer to the address of the first element. Consider the following declarations:
int list ;
Consider the assignment
ptr = list;
which assigns the address of list to ptr, because an array name without a subscript is interpreted as the base address of the array. Given this assignment,
the following are true:
• *(ptr 1) is equivalent to list.
• *(ptr index) is equivalent to list[index].
• ptr[index] is equivalent to list[index].
It is clear from these statements that the pointer operations include the same scaling that is used in indexing operations. Furthermore, pointers to arrays can
be indexed as if they were array names.
Pointers in C and C can point to functions. This feature is used to pass functions as parameters to other functions. Pointers are also used for parameter passing.