If you declare a variable, its name is a direct reference to its value. If you have a pointer to a variable, or any other object in memory, you have an indirect reference to its value. If p is a pointer, the value of p is the address of the object. *p means “apply the indirection operator to p”; its value is the value of the object that p points to. (Some people would read it as “Go indirect on p.”)
*p is an lvalue; like a variable, it can go on the left side of an assignment operator, to change the value. If p is a pointer to a constant, *p is not a modifiable lvalue; it can’t go on the left side of an assignment.
Consider the following program. It shows that when p points to i, *p can appear wherever i can.
C program
#include <stdio.h>
#include <conio.h>
int main() {
int i;
int *p;
i = 5;
p = &
i;
/* now *p == i */
printf("i=%d, p=%P, *p=%d\n", i, p, *p);
*p = 6;
/* same as i = 6 */
printf("i=%d, p=%P, *p=%d\n", i, p, *p);
return 0;
}
A circular list that uses infinite indirection
#include <stdio.h>
struct circ_list {
char value[ 3 ];
/* e.g., "st" (incl '') */
struct circ_list *next;
}
;
struct circ_list suffixes[] = {
"th", & suffixes[ 1 ],
/* 0th */
"st", & suffixes[ 2 ],
/* 1st */
"nd", & suffixes[ 3 ],
/* 2nd */
"rd", & suffixes[ 4 ],
/* 3rd */
"th", & suffixes[ 5 ],
/* 4th */
"th", & suffixes[ 6 ],
/* 5th */
"th", & suffixes[ 7 ],
/* 6th */
"th", & suffixes[ 8 ],
/* 7th */
"th", & suffixes[ 9 ],
/* 8th */
"th", & suffixes[ 0 ],
/* 9th */
}
;
#define MAX 20
int main() {
int i = 0;
struct circ_list *p = suffixes;
while (i value );
++i;
p = p->next;
}
return 0;
}
Each element in suffixes has one suffix (two characters plus the terminating NUL character) and a pointer to the next element. next is a pointer to something that has a pointer, to something that has a pointer, ad infinitum.
The example is dumb because the number of elements in suffixes is fixed. It would be simpler to have an array of suffixes and to use the i%10’th element. In general, circular lists can grow and shrink.