HP C A.06.05 Reference Manual
Expressions and Operators
Pointer Operators (*, ->, &)
Chapter 5118
Pointer Operators (*, ->, &)
Syntax
*
ptr_exp
Dereferences a pointer. That is, finds the contents stored at the virtual
address that
ptr_exp
holds.
ptr->member
Dereferences a
ptr
to a structure or union where
member
is a member of
that structure or union.
&
lvalue
Finds the virtual address where the lvalue stored.
Description
A pointer variable is a variable that can hold the address of an object.
Assigning an Address Value to a Pointer
To declare a pointer variable, you precede the variable name with an asterisk. The following
declaration, for example, makes ptr a variable that can hold addresses of long int variables:
long *ptr;
The data type, long in this case, refers to the type of variable that ptr can point to. To assign
a pointer variable with the virtual address of a variable, you can use the address-of operator
&. For instance, the following is legal:
long *ptr;
long long_var;
ptr = &long_var; /* Assign the address of long_var to ptr. */
But this is illegal:
long *ptr;
float float_var;
ptr = &float_var; /* ILLEGAL - because ptr can only store the
address of a long int. */
The following program illustrates the difference between a pointer variable and an integer
variable.
/* Program name is "ptr_example1". */
#include <stdio.h>
int main()