Pages

Sunday, November 21, 2010

Pointer and Arrays

Pointers are arrays are closely related with each other. The relationship between an array and a pointer is in fact so close that it can be said that "an array is actually a pointer, pointing to the first element in the collection." Till this time we have learnt that an array is a collection of variable of same datatype, therefore to know that array is actually a pointer you can find hard to digest. Before going on its details let's discuss some other topics first.

Array of pointers
If you have just 1 or 2 variables to point you can easily declare equal number of variable to point them, what if you have say 20 to 30 variable to point ? Will you declare so many variables, what if the variables are even more ? It would be impractical to say that one will declare 50 pointers or 100 pointers, so how will we create so many pointers? The answer is simple - Array of pointers. Just as an array of variable can store multiple variables, an array of pointers can store multiple pointers. This will not only save us from the trouble of declaring so many pointers but will also make the program very short. Let's see how it is done

Example 1
#include
#include
void main()
{
    int a=1,b=2,c=3,d=4,e=5;
    int *ptr[5];
    p[0]=&a; //storing the address of a in p[0]
    p[1]=&b;
    p[2]=&c;
    p[3]=&d;
    p[4]=&e;
    // printing all the variables
    printf("\nA %d",a); // 1
    printf("\nB %d",b); // 2
    printf("\nC %d",c); // 3
    printf("\nD %d",d); // 4
    printf("\nE %d",e); // 5
    // printing their address using the pointer array
    printf("\nP[0] %d",p[0]);   // address of a
    printf("\nP[1] %d",p[1]);  // address of b
    printf("\nP[2] %d",p[2]);  // address of c
    printf("\nP[3] %d",p[3]);  // address of d
    printf("\nP[4] %d",p[4]);  // address of e
    //printing the values of the variables through the array
    printf("\nP[0] %d",*p[0]);   // value of a
    printf("\nP[1] %d",*p[1]);  // value of b
    printf("\nP[2] %d",*p[2]);  // value of c
    printf("\nP[3] %d",*p[3]);  // value of d
    printf("\nP[4] %d",*p[4]);  // value of e
   
}


Description
In the code given above, we have 5 different variables. To point these variables we need 5 different pointers. Instead of declaring 5 pointers, we declare an array of 5 pointers named "p". Now to store the address of the first variable "a" in index 0 of array p, we use a statment - "p[0]=&a". This will put the address of "a" in the first element of our array pointer. Similarly the addresses of rest of the variables (b,c,d,e) are stored in index 1,2,3,4 respectively. Once the array has all the required address we can easily print all the address or the values by using simple "printf()" statments.


Example 2  - Same program using loop
#include
#include
void main()
{
    int a=1,b=2,c=3,d=4,e=5,i;
    int *ptr[5];
    p[0]=&a; //storing the address of a in p[0]
    p[1]=&b;
    p[2]=&c;
    p[3]=&d;
    p[4]=&e;
    // printing all the variables
    printf("\nA %d",a); // 1
    printf("\nB %d",b); // 2
    printf("\nC %d",c); // 3
    printf("\nD %d",d); // 4
    printf("\nE %d",e); // 5
    // printing their address using the pointer array
    for(i=0;i<5;i++)
        printf("\nP[i] %d",p[i]);
   
    //printing the values of the variables through the array
    for(i=0;i<5;i++)
        printf("\nP[0] %d",*p[i]);   // value of a
       
}

Description
This is the same program but it uses a loop to print the address as well as the values. This will make the program quite small specially if the array is too large. The first loop prints the addresses whereas the second one is used to print values.

0 comments:

Post a Comment