Pointer Operators

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 & Pointer Operator

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 * Pointer Operator

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.




C(s)C++ Programmer's Reference
C Programming on the IBM PC (C Programmers Reference Guide Series)
ISBN: 0673462897
EAN: 2147483647
Year: 2002
Pages: 539

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net