Lesson 9: Pointers.

Prev || Home || Next

Bar_line.gif (11170 bytes)

   Pointers are another important feature of C language. They are a powerful tool and handy to use due to following reasons.

    1) They enable use to access a variable that defined outside the function.

    2) Pointers are more efficient in handling data tables.

    3) Pointers reduce the length and complexity of the program.

    4) They increase the execution speed.

        As you know computers store in their memory  instructions as well as the values of variables in the program. The computers memory is a sequentional collection of storage cells. Each cell has a number called address associated with it. Typically, the addresses are numbered consecutively starting from zero. Whenever we declare a variable, the system allocates, somewhere in the memory, alocation to hold the value of the variable. Consider the following statement.

    int quantity=179;

   Here a location in the memory is allocated whose name is quantity and the value 179 is put in it. Let us assume that the syatem has chosen the address location 5000 for quantity.

    During execution of the program, the system always associates the name quantity with thw address 5000.(This is something similar to having a house number as well as a house name.) We have access to the value 179 by using either the name quantity or the address 5000. Since memory addresses are simply numbers, they can be assigned to some variables which can be stored in memory .

    Such variables that hold memory addresses are called pointers. A pointer is, therefore, nothing but a variable that contains an address which is a location of another variable in memory. Suppose we assign the address of quantity to a variable p. We mzay access the value of quantity by using the value of p and therefore we say that the variable p 'points' to the variable quantity. Thus, pgets the name 'pointer'. The link between p and quantity can be visualised in the following table:

Variable Value Address
quantity 179 5000
p 5000 5048

    The address of a variable is not known to us immediately. However, we can know this with the help of the operator &(ampersand) avilable in C. The operator & immediately preceding a variable returns the adderess of the variable associated with it.

    For example, the statement

    p=&quantity;

    would assign the address 5000(the location of quantity) to the variable p.

   Declaring and Initializing Pointers:

       In C, every variable must be declared for its type. For ex. int *p declares the variable p as a pointer variable to an integer data type.

    1) The asterik(*) tells that the variable p is an pointer variable.

    2) This pointer variable p points to an integer data type.

    3) The type int refers to data type of the variable being pointed by p & not the type of the value pointer.

    Similarly the statement float *x; declares x as a pointer to a floating type variable i.e value at address contained in x is a float.

    Program:- To access variables using pointer.

    main()

    {

        int x,y;

        int *ptr;

        x=10;

        ptr=&x;

        y=*ptr;

        printf("Value of x is=%d\n",x);

        printf("%d is stored at address %u\n",x,&x);

        printf("%d is stored at address %u\n",*&x,&x);

        printf("%d is stored at address %u\n",*pte,pte);

        printf("%d is stored at address %u\n",y,&*ptr);

        printf("%d is stored at address %u\n",ptr,&ptr);

        printf("%d is stored at address %u\n",y,&y);

        *ptr=25;

        printf("\nNow x = %d\n",x);

    Output:-

                Value of x is 10

                10 is stored at address 4104

                10 is stored at address 4104

                10 is stored at address 4104

                10 is stored at address 4104

                4104 is stored at address 4106

                10 is stored at address 4108

                Now x=25

    Program:-

    main()

    {

        int i=30;

        int *j,**k;

        j=&i;

        k=&j;

        printf("Address of i=%d%d%d\n",&i,j*k);

        printf("Address of j=%d%d\n",&j,k);

        printf("Address of k=%d\n",&k);

        printf("Value of i=%d%d%d%d\n",i,*(&i),*j,**k);

    }

       Pointers are much useful in function call. Function can be called in two ways, either by passing values of arguments or by passing addresses of arguments. The former is called "Call by value" and the latter a"Call by reference." These calss are illustrated below

/* Call by value */                                                                     /* Call by refrence */

main()                                                                                      main()   

{                                                                                             {

    int a=10,b=20;                                                                         int a=10,b=20;

    swapv(a,b);                                                                             swapr(a,b);

    printf("a=%d b=%d\n"a,b);                                                     printf(a=%d b=%d\n",a,b);

}                                                                                             }

swapv(x,y)                                                                               swapr(x,y)  

int x,y;                                                                                     int x,y;

{                                                                                             {    

    int t;                                                                                         int t;

    t=x;                                                                                         t=*x;

    x=y;                                                                                        *x=*y;

    y=t;                                                                                         *y=t;

    printf("x=%d y=%d\n"x,y);                                                     printf("x=%d *y=%d\n",*x,*y);

}                                                                                             }

Output:-                                                                                     Output:-

            x=20 y=10                                                                     *x=20 *y=10

            a=10 b=20                                                                       a=20 b=10     

Note:                                                                                           Note:

    Changes made in x and y in swapv() are                                    Changes made in swapr() reflected

    not using x and y are reflected back in a nad b.                          back in a and b.

 Bar_line.gif (11170 bytes)

  Prev || Home || Next