Page 170 - DCAP507_SYSTEM_SOFTWARE
P. 170

System Software




                    Notes          We discover the value pointed to by a pointer using the “contents-of” operator, *. Placed in front
                                   of a pointer, the * operator accesses the value pointed to by that pointer. In other words, if ip is
                                   a pointer, then the expression *ip gives us whatever it is that's in the variable or location pointed
                                   to by ip.


                                          Example: We could write something like
                                          printf("%d\n", *ip);
                                   which would print 5, since ip points to i, and i is (at the moment) 5.

                                   (You may wonder how the asterisk * can be the pointer contents-of operator when it is also the
                                   multiplication operator. There is no ambiguity here: it is the multiplication operator when it
                                   sits between two variables, and it is the contents-of operator when it sits in front of a single
                                   variable. The situation is analogous to the minus sign: between two variables or expressions it's
                                   the subtraction operator, but in front of a single operator or expression it's the negation operator.
                                   Technical terms you may hear for these distinct roles are unary and binary: a binary operator
                                   applies to two operands, usually on either side of it, while a unary operator applies to a single
                                   operand.)
                                   The contents-of operator * does not merely fetch values through pointers; it can also set values
                                   through pointers. We can write something like
                                          *ip = 7;
                                   which means ``set whatever ip points to 7.'' Again, the * tells us to go to the location pointed to
                                   by ip, but this time, the location isn't the one to fetch from--we're on the left-hand sign of an
                                   assignment operator, so *ip tells us the location to store to.

                                   If we called printf("%d\n", *ip) again, it would now print 7.
                                   At this point, you may be wondering why we're going through this rigmarole--if we wanted to
                                   set i to 7, why didn't we do it directly? We'll begin to explore that next, but first let's notice the
                                   difference between changing a pointer (that is, changing what variable it points to) and changing
                                   the value at the location it points to. When we wrote *ip = 7, we changed the value pointed to by
                                   ip, but if we declare another variable j:
                                          int j = 3;
                                   and write

                                          ip = &j;
                                   we've changed ip itself
                                   We have to be careful when we say that a pointer assignment changes ''what the pointer points
                                   to.'' Our earlier assignment
                                          *ip = 7;
                                   changed the value pointed to by ip, but this more recent assignment
                                          ip = &j;

                                   has changed what variable ip points to. It's true that ''what ip points to'' has changed, but this
                                   time, it has changed for a different reason. Neither i (which is still 7) nor j (which is still 3) has
                                   changed. (What has changed is ip's value.) If we again call
                                          printf("%d\n", *ip);
                                   this time it will print 3.




          164                               LOVELY PROFESSIONAL UNIVERSITY
   165   166   167   168   169   170   171   172   173   174   175