Pointer Operators
• * (indirection/dereferencing operator)
- Returns a synonym/alias of what its operand points to
- *yptr returns y (because yptr points to y)
- * can be used for assignment
• Returns alias to an object
*yptr = 7; // changes y to 7
- Dereferenced pointer (operand of *) must be an lvalue
(no constants)
• * and & are inverses
- They cancel each other out
int rate;
int *p_rate;
rate = 500;
p_rate = &rate;
1000 1004 1008 1012
Memory 1008 500
p_rate rate
/* Print the values */
printf(“rate = %d\n”, rate); /* direct access */
printf(“rate = %d\n”, *p_rate); /* indirect access */
/* Using the & and * operators */
#include <stdio.h> The address of a is the value
of aPtr.
int main()
{
int a; /* a is an integer */
The * operator returns an
int *aPtr; /* aPtr is a pointer to an integer */
alias to what its operand
a = 7; points to. aPtr points to a,
aPtr = &a; /* aPtr set to address of a */ so *aPtr returns a.
printf( "The address of a is %p\nThe value of aPtr is %p", &a, aPtr );
printf( "\n\nThe value of a is %d\nThe value of *aPtr is %d", a, *aPtr );
Notice how * and &
printf( "\n\nShowing that * and & are inverses of are inverses
each other.\n&*aPtr = %p\n*&aPtr = %p\n", &*aPtr, *&aPtr );
}
return 0;
Program Output
The address of a is 0012FF88
The value of aPtr is 0012FF88
The value of a is 7
The value of *aPtr is 7
Showing that * and & are inverses of each other.
&*aPtr = 0012FF88
*&aPtr = 0012FF88