THE and * Operators

A pointer is a variable that represents the location of a data item, such as a variable or an array element. Pointers are used frequently in C, as they have a number of useful applications. For example, pointers can be used to pass information back and forth between a function and its reference point. Pointers provide a way to return multiple data items from a function via function arguments to be specified as arguments to a given function.

Pointers are also closely associated with arrays and therefore provide an alternate way to access individual array elements.

Within the computer?s memory, every stored data item occupies one or more adjacent memory cells. The number of memory cells required to store a data item depends on the type of data item. For example, a single character will be stored in 1 byte of memory integer usually requires two adjacent bytes, a floating point number may require four adjacent bytes.

Suppose V is a variable that represents some particular data item. The compiler will automatically assign memory cells for this data item. The data item can be accessed if we know the location of the first memory cell. The address of V?S memory location can be determined by the expression & V, where & is a unary operator, called the address operator, that evaluates the address of its operand.

Now let us assign the address of V to another variable, PV. Thus,

This new variable is called a pointer to V, since it “Points” to the location where V is stored in memory. Remember, however, that PV represents V?s address, not its value. Thus, Pv is called pointer variable.

Relationship between PV and V (where PV = & V and V = * PV)

The data item represented by V can be accessed by the expression *PV where * is a unary operator, that operates only on a pointer variable. Therefore, PV and V both represent the same data item. Furthermore, if we write PV = &V and U = PV, then u and v will both represent the same values i.e., the value of V will indirectly be assigned to u.

Example :

int quantity = 179 ;

The statement instructs the system to find a location for the integer quantity and puts the value 179 in that location. Let us reassume that the system has chosen the address location 5000 for quantity.

Representation of a Variable

Remember, since a pointer is a variable, its value is also stored in the memory in another location.

The address of P can be assumed to be 5048.

Share
Tweet
Share
Pin
+1