HP C A.06.05 Reference Manual

Expressions and Operators
Pointer Operators (*, ->, &)
Chapter 5120
The expression *p_ch is interpreted as “Take the address value stored in p_ch and get the
value stored at that address.” This gives us a new way to look at the declaration. The data
type in the pointer declaration indicates what type of value results when the pointer is
dereferenced. For instance, the declaration
float *fp;
means that when *fp appears as an expression, the result will be a float value.
The expression *fp can also appear on the left side of an expression:
*fp = 3.15;
In this case, we are storing a value (3.15) at the location designated by the pointer fp. This is
different from
fp = 3.15;
which attempts to store the address 3.15 in fp. This, by the way, is illegal, because addresses
are not the same as floating-point values.
When you assign a value through a dereferenced pointer, make sure that the data types agree.
For example:
/* Program name is "ptr_example3". */
#include <stdio.h>
int main(void)
{
float f = 1.17e3, g;
int *ip;
ip = &f;
g = *ip;
printf("The value of f is: %f\n", f);
printf("The value of g is %f\n", g);
}
The result is
The value of f is: 1170.000000
The value of g is: 1150435328.000000
In the preceding example, instead of getting the value of f, g gets an erroneous value because
ip is a pointer to an int, not a float. The HP C compiler issues a warning message when a
pointer type is unmatched. If you compile the preceding program, for instance, you receive the
following message: