Sunday, November 16, 2008

Pointers in C

POINTERS

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 usuallyn 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, PV = & V

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.

DYNAMIC DATA STRUCTURES IN C

address of V value of V

PV V

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.

Quantity
179
5000
Variable
Value
Address


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.
Variable
Quantity
P
Value
179
5000
Address
5000
5048

Pointer as a variable

Declaring and initializing Pointers

Since pointer variables contain addresses that belong to a separate data type, they must be declared as pointers before we use them. The declaration of a pointer variable takes the following form:

data type * Pt _ name


This tells the compiler three things about the variable Pt_name.

1. The * tells that the variable Pt_name is a pointer variable.

2. Pt_name needs a memory location.

3. Pt_name ponts to a variable of type data type.

Example : int * P ;

Declares the variable P as a pointer variable that points to an integer data type.
float * y ; declares y as a pointer to a floating point variable.
Once pointer variable has been declared, it can be made to point to a variable using an assignment statement such as

P = & quantity ;

which causes P to point to quantity. P contains the address of quantity. This is known as pointer initialization.

Pointer expressions

Like other variables, pointer variables can be used in expressions. For example, if P1 and P2 are properly declared and initialized pointers, then the following statements are valid.

1) Y = * P1 ;

2) Sum = Sum + * P1 ;

3) Z = S - * P2 / * P1 ;

4) * P2 = * P2 + 10 ;

Note that there is a blank space between / and * in the item 3 above.

If P1 and P2 are pointers then the expressions such as,

P1 + 4 , P2 - 2 , P1 - P2 , P1 ++ , — P2 are allowed

also,

Sum =Sum + *P2 ;

P1 ++ ;

- -P2 ;

P1 > P2

P1 = = P2

P1 ! = P2

are all allowed expressions.

The expressions such as, P1 / P2 or P1 * P2 or P1/3 are not allowed.

Pointer assignments

After declaring a pointer, pointer is assigned a value, so that it can point to a particular variable.

eg. int * P ;

int i ;

P = & i ;

This is called assignment expression in which pointer variable P is holding the address of i.

Pointer arithmetic

Two pointer values can be added, multiplied, divided or subtracted together.
eg. if int i ;

int j ;

int * P , * q ;

i = 5 , j = 10 ;

Now, various pointer arithmetic can be performed

eg. * j = * i + * j ;

The value of variable j is changed from 10 to 15.

* j = * j - * i ;

The value of variable j is changed from 10 to 5.

* i = * i ** j ;

The value of i is changed from 5 to 50 ;

Consider another example,

if there is array and a pointer is pointing to it
int i [10] ;

int * P ;

P = i ;

Now, arithmetic operations like

P = P + 4 ;

Will move the pointer P from the starting address of the array to the fourth subscript of array.

Similarly, if P1 and P2 are both pointers to the same array, then P2 - P1 gives the number of elements between P1 and P2.


arithmetic operations like

P1/P2 or P1 x P2 or P/3 are not allowed.

Pointer Comparison

In addition to arithmetic operations, pointers can also be compared using the relational operators.

The expressions such as

P1 > P2 , P1 = = P2 , P1 ! = P2 are allowed.

However, any comparison of pointers that refer to separate and unrelated variables make no sense. Comparisons can be used meaningfully in handling arrays and strings.
The dynamic allocation functions - malloc( ) and calloc( )

Most often we face situations in programming where the data is dynamic in nature. That is, the number of data items keep changing during execution of the program. For example, consider a program for processing the list of customers of a company. The list grows when names are added and shrinks when names are deleted. When list grows we need to allocate more memory space to the list to accommodate additional data items. Such situations can be handled more easily and effectively by using what is called dynamic data structures.

No comments: