HP C/iX Reference Manual (31506-90011)

Chapter 9 157
HP C/iX Implementation Topics
Long and Short Pointers
Long and Short Pointers
HP C defines two classes of data pointers: short and long pointers. A short pointer is a
32-bit pointer that contains the offset of an object local to its process. A long pointer is a
64-bit pointer that may point to an object outside its current process space. The high-order
32 bits of a long pointer contain a space ID number, and the low-order 32 bits represent an
offset within the space defined by that space ID.
A short pointer can point to any addressable object within its own process space. This
includes local and global variables, function parameters, and heap variables. Long pointers
may point to any addressable object on the system. This includes objects that are outside
the space of the current process. Long pointers are useful for calling system intrinsics that
require long pointer parameters (such as PRINT), and for accessing user-mapped files.
Miscellaneous Pointer Features
Pointers to functions should not be compared using relational operators because the
pointers represent external function labels and not actual addresses.
Dereferencing a pointer that contains an invalid value results in a trap if the address
references protected memory or if the address is not properly aligned for the object
being referenced.
A declaration of a pointer to an undefined structure tag is allowed, and the tag need not
be defined in the source module unless the pointer is used in an expression.
fp() is equivalent to in an expression when fp is of type pointer to function.
Declaring Long Pointers
You declare long pointer variables or parameters using the normal (short pointer) syntax
except you need to substitute a caret (^) for the asterisk(*). Refer to chapter 3 for
information on declaring pointers.
For example:
/* int_pointer is a long pointer to an integer */
int ^int_pointer;
/* char_pointer is a long pointer to a character */
char ^char_pointer;
Long pointers may not be declared with initializers. Therefore, a statement such as:
char ^long_ptr = "some string";
is not allowed. Also, long function pointers are not allowed.
Using Long Pointers
Long pointers are dereferenced the same as short pointers. If p is declared as a long
pointer to an int, such as int ^p;, then *p returns the contents of that integer. If p is
declared as a pointer to a structure containing a member mem, p->mem will return the