* Unary Indirection operator; Binary Multiplication
*ptr Indirection (used as unary operator)
ptr any pointer type or expression
exp1 * exp2 Multiplication (used as binary operator)
exp1 any int or float expression
exp2 any int or float expression
The * operator has two different meanings, depending upon its
context.
As a binary operator, * performs multiplication. The result depends
upon the operators and follows the usual arithmetic conversions.
Here are two examples:
i = 6 * 7; /* i == 42 */
x = 3.4 * 5; /* x == 17.0 */
As a unary operator, * dereferences a pointer; in other words, *ptr
refers to the element pointed to by ptr. The argument for * is any
pointer type or expression; the resulting expression is equivalent to
an object of the type that ptr points to. Example: a = *myptr;
------------------------- Pointers to Functions -------------------------
Perhaps one of the more confusing pointers is the function pointer,
which points to a function rather than to a data object. It is much
easier to use than to describe, so here is an example that shows all
the steps for declaring and using a pointer to a function:
int func1(); /* Declares func1() and func2() */
int func2(); /* as functions returning int */
int (*funcptr)(); /* Declares funcptr as a pointer */
/* to a function returning an int */
if (x < 2)
funcptr = func1; /* funcptr points to func1() */
else /* or */
funcptr = func2; /* funcptr points to func2() */
i = (*funcptr)(10,3); /* Call func1() or func2() */
The first two statements declare that func1() and func2() are
functions (not variables) that return ints. The next statement is
the important one: it declares a pointer--funcptr--to a function; the
function that funcptr points to returns an int.
The if statement assigns the address of either func1() or func2() to
funcptr. (Note that the name of a function is equivalent to the
address of that function; just as the name of an array is equivalent
to the address of the array.) Finally, the last line makes a call to
one of those two functions, with `10' and `3' as arguments. A pointer
to a function is especially useful when you want to pass functions as
arguments of a function.
Seealso:
This page last updated on Fri Nov 30 10:48:32 MSK 2001
Copyright © 1992-2001, Vitaly Filatov, Moscow, Russia
Webmaster