|
|
The two pointer operators are * and &. A pointer is an object that contains the address of another object. Or, put differently, an object that contains the address of another object is said to “point to” the other object.
The & operator returns the address of the object it precedes. For example, if the integer x is located at memory address 1000, then
p = &x;
places the value 1000 into p. The & can be thought of as “the address of.” For example, the previous statement could be read as “place the address of x into p.”
The * is the indirection operator. It uses the current value of the variable it precedes as the address at which data will be stored or obtained. For example, the following fragment
p = &x; /* put address of x into p */ *p = 100; /* use address contained in p */
places the value 100 into x. The * can be remembered as “at address.” In this example, it could be read, “place the value 100 at address p.” Since p contains the address of x, the value 100 is actually stored in x. In words, p is said to “point to” x. The * operator can also be used on the right-hand side of an assignment. For example,
p = &x; *p = 100; z = *p / 10;
places the value of 10 into z.
|
|