HP C A.06.05 Reference Manual
Expressions and Operators
Pointer Operators (*, ->, &)
Chapter 5 119
{
int j = 1;
int *pj;
pj = &j; /* Assign the address of j to pj */
printf("The value of j is: %d\n", j);
printf("The address of j is: %p\n", pj);
}
If you run this program (in 32-bit mode), the output looks something like this:
The value of j is: 1
The address of j is: 7b033240
Dereferencing a Pointer
To dereference a pointer (get the value stored at the pointer address), use the * operator. The
program below shows how dereferencing works:
/* Program name is "ptr_example2". */
#include <stdio.h>
int main(void)
{
char *p_ch;
char ch1 = 'A', ch2;
printf("The address of p_ch is %p\n", &p_ch);
p_ch = &ch1;
printf("The value of p_ch is %p\n", p_ch);
printf("The dereferenced value of p_ch is %c\n",
*p_ch);
}
The output from this program looks something like this:
The address of p_ch is 7b033240
The value of p_ch is 7b033244
The dereferenced value of p_ch is A
This is a roundabout and somewhat contrived example that assigns the character A to both
ch1 and ch2. It does, however, illustrate the effect of the dereference (*) operator. The variable
ch1 is initialized to A. The first printf() call displays the address of the pointer variable
p_ch. In the next step, p_ch is assigned the address of ch1, which is also displayed. Finally,
the dereferenced value of p_ch is displayed and ch2 is assigned to it.