#include int main (void) { /* a,b,e are declared as integers. However, c and d are pointers to integers - notice star before identifiers. This demonstrates the 1st usage of "*" - declaring a pointer variable */ int a, b, *c, *d, e; a = 10; /* Below, "star" is used simply as multiplication */ b = a * 4; c = &a; /* address of a goes into c, i.e. c points to 10 */ d = &b; /* address of b goes into d, i.e. d points to 40 */ /* Next line is using "star" to retrieve the value of the variable pointed by the pointer. This is the 2nd usage of "*" - dereference */ e = *c + *d; /* Value of *c is a and value of *d is b.Therefore, e=10+40=50.*/ /* The 1st format string matches the 1st value to be printed, the 2nd format string matches the 2nd value, and so on ... */ printf ("a= %d, b= %d, *c= %d *d= %d e=*c+*d=%d\n", a, b, *c, *d, e); *d = a; /* value pointed by d is assigned a, i.e. *d becomes 10. But d holds the address of b. As a _side-effect_, value of b changes too ! */ printf ("a= %d, b=*d=%d, *c= %d *d=a=%d e= %d \n", a, b,*c,*d,e); d = &a; /* d no longer points to b, but now it points to a. However, nothing else changes, just direction of pointer d changed.*/ printf ("a= %d, b= %d, *c= %d *d= %d e= %d \n", a, b,*c,*d,e); /* Operation (a % b) produces remainder from dividing a=10 by b=10, i.e., 0*/ *c = *d - a % b + *c; /* We change the value pointed at by c, but since c is a pointer to a, and d was also assigned to point to a, as a side-effect, we also change both a and the value pointed by d.*/ printf ("Do you understand why\n"); printf ("a= %d, b= %d, *c= %d *d= %d e= %d ?\n", a, b,*c,*d,e); return (0); } /* Output: a= 10, b= 40, *c= 10 *d= 40 e=*c+*d=50 a= 10, b=*d=10, *c= 10 *d=a=10 e= 50 a= 10, b= 10, *c= 10 *d= 10 e= 50 Do you understand why a= 20, b= 10, *c= 20 *d= 20 e= 50 ? */