Thursday, January 05, 2012

ARRAY AND POINTER

Introduction

Array itself uses pointer internally. Like other variables, address of an array can also be stored in a pointer variable or a Array of Pointers can be made. Address of the first element of the array is called the Base Address.

Method 1

    int a[5]={1,2,3,4,5};

This array contains five elements, to print the address of each one we can write :

    for( i = 0 ; i < 5 ; i++ )
        printf("Address of %d is %u  \n\n",a[i], &a[i] );

Method 2

Here each pointer variable is initialized with the address of array elements.

    int *p0 = &a[0];
    int *p1 = &a[1];


    printf("Address of %d is %d \n\n", *p0, p0 );

Method 3

Array of Pointers

We can make an array of pointer where addresses of other array elements or variables can be stored like :

int *p[5]={a,a+1,a+2,a+3,a+4};

Here a means the base address or the address of the first value, a+1 means the next address that is the address of the second value, a+2 means the third address and so on. To print them we can write :

    for( i = 0 ; i < 5 ; i++ )
        printf("Address of %d is %d  \n\n",*p[i], p[i]  );

Method 4

Simply know the dimension of the array and take the base address into a pointer variable
like :

int *ranty = a;

Now to print each values with address we can write :

    for( i = 0 ; i < 5 ; i++ )
    {
        printf("Address of %d is %d \n\n", *ranty, ranty );
        ranty++;
    }

When value of i is 0, value at ranty that is value and address of a[0] gets printed.
ranty++ means switching over to the next memory location that is value and address of a[1] gets printed. It would be carried on till the loop gets ended, whose condition is 1 less than the dimension of the array.

A Program Demonstrating the above points :

#include"stdio.h"
void printline(void);
void main()
{
    int a[5]={1,2,3,4,5} , i;

    printf("\nValues of the Array are : ");

        for( i = 0 ; i < 5 ; i++ )

            printf("%d ",a[i]);
   
    printf("\n");

    printline();

    printf("\nPRINT VALUES WITH ADDRESSES ! \n\n");

    printline();

    printf("\tMETHOD 1 \n\n");
   
        for( i = 0 ; i < 5 ; i++ )

            printf("Address of %d is %u \n\n",a[i], &a[i] );

    printline();
   
    printf("\tMETHOD 2 \n\n");

    int *p0 = &a[0]; // A pointer variable storing the address of first element.
    int *p1 = &a[1];
    int *p2 = &a[2];
    int *p3 = &a[3];
    int *p4 = &a[4];

    printf("Address of %d is %d \n\n", *p0, p0 );
    printf("Address of %d is %d \n\n", *p1, p1 );
    printf("Address of %d is %d \n\n", *p2, p2 );
    printf("Address of %d is %d \n\n", *p3, p3 );
    printf("Address of %d is %d \n\n", *p4, p4 );

    printline();

    printf("\tMETHOD 3 \n\n");
   
    int *p[5]={a,a+1,a+2,a+3,a+4};  // Array of Pointer

        for( i = 0 ; i < 5 ; i++ )

            printf("Address of %d is %d \n\n",*p[i],p[i] );

    printline();

    printf("\tMETHOD 4 \n\n");

        int *ranty = a;  // Taking the Base Address or the First Address

        for( i = 0 ; i < 5 ; i++ )
        {
            printf("Address of %d is %d \n\n",*ranty,ranty );

            ranty++;
        }

    printline();
}

void printline()
{  
    int i;

    for( i=0 ; i<60 ; i++ )
        printf("_");

    printf("\n\n");
}

Another Example for Array of Pointers :

#include"stdio.h"
void main()
{
    int a = 5, b = 10, c = 15, i;

    int *pt[3] = {&a, &b, &c};

    for( i = 0 ; i < 3 ; i++ )

         printf("Values of the Array are %d  \n", pt[i] );

    printf("a + b = %d  \n", *pt[0] + *pt[1] );
    printf("c - b = %d  \n", *pt[2] - *pt[1] );
}

No comments:

Post a Comment

Followers