Topics

6/recent/ticker-posts

Pointer to Arrays (Pointer part 2)


 

            Pointer to arrays
 

Declaration of the pointer to an array:

int   a[5];

 

                         a[0]             a[1]               a[2]             a[3]        a[4]         

 

 

 

 

 

Addressà1000   1001  1002  1003 1004  1005 1006 1007  1008 1009

 

Since first byte address is an address any variable, then address of a[0] è 1000

 

 

&a[0] è 1000

&a[1] è 1002

&a[2] è 1004

&a[3] è 1006

&a[4] è 1008

 

First byte address is a Address of array

 

Array name itself is an address of array ( base address )

 

accessing address of array elements.

 

            a --> base address --> 1000

           

            a + 0 --> (1000 + 0*2 ) --> 1000 --> address of 1st element  -->&a[0]

           

            a + 1 --> (1000 + 1*2 ) --> 1002 --> address of 2nd element  -->&a[1]

 

            a + 2 --> (1000 + 2*2 ) --> 1004 --> address of 3rd element  -->&a[2]

 

            a + 3 --> (1000 + 3*2 ) --> 1006 --> address of 4th element  -->&a[3]

 

            a + 4 --> (1000 + 4*2 ) --> 1008 --> address of 5th element  -->&a[4]

 

 

Here 2 is a multiplication factor depends on data type. It is called as scale factor

This value is equal to sizeof  data type of variable or pointer

 


Program to display address of array elements:

 

#include<stdio.h>

#include<conio.h>

int main()

{

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

 int i;

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

 {

  printf("\n%u %u ",(a+i), &a[i]);

 }

 getch();

 

}

 


 Accessing value thorough the address:

 

            *(a + 0) --> *(1000 + 0*2 ) --> *(1000) --> value of a[0]

 

            *(a + 1) --> *(1000 + 1*2 ) --> *(1002) --> value of a[1]

 

            a[0] --> can be written as  *(a+0);

            a[1] --> can be written as  *(a+1);

            a[2] --> can be written as  *(a+2);

            a[3] --> can be written as  *(a+3);

            a[4] --> can be written as  *(a+4);

 

#include<stdio.h>

#include<conio.h>

main()

{

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

 int i;

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

 {

  printf("%d  ",*(a+i));

 }

 getch();

 

}

 

O/P:     1          2          3          4          5



#include<stdio.h>

#include<conio.h>

main()

{

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

 int i;

 for(i=4;i>=0;i--)

 {

   printf("%d  ",*(a+i));

 }

 getch();

 

}

 

O/P:     5          4          3          2          1



Program to display array address and its elements address

 

#include<stdio.h>

main()

{

 int a[5],i;

            printf("\n aadres of array a = %u",a);

 

 

            printf("\n address of array elements\n ");

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

            {

            printf("%u  ",&a[i]);

            }

 getch();

 }

 

 

Declaring pointer to array:

 

int a[5];

int *ptr;

 

     ptr=a;  à ptr contains address of array.  Array name itself is an address of array

 

     ptr=&a à invalid, since Array name itself is an address of array

 

 

Accessing address of array elements through the pointer.

 

ptr à 1000  à base address of array

 

(ptr+0) à ( 1000 + 0* 2) à 1000 à address of 1st element

 

(ptr+1) à ( 1000 + 1* 2) à 1002 à address of 2nd  element

 

(ptr+2) à ( 1000 + 2* 2) à 1004 à address of 3rd   element

 

(ptr+3) à ( 1000 + 3* 2) à 1006 à address of 4th   element

 

(ptr+4) à ( 1000 + 4* 2) à 1008 à address of 5th   element

 

Accessing  array elements through the pointer.

 

*(ptr+0) à *( 1000 + 0* 2) à *(1000) à value of 1st element

 

*(ptr+1) à *( 1000 + 1* 2) à *(1002) à value of 2nd  element

 

*(ptr+2) à *( 1000 + 2* 2) à *(1004) à value of 3rd   element

 

*(ptr+3) à *( 1000 + 3* 2) à *(1006) à value of 4th   element

 

*(ptr+4) à *( 1000 + 4* 2) à *(1008) à value of 5th   element

 

#include<stdio.h>

#include<conio.h>

int main()

{

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

 int i,*ptr;

 for(ptr=a,i=0;i<5;i++)

 {

  printf("\n%d ",*(ptr+i));

 }

 getch();

 

}

 

 


Note: Here in this website all the programs given here are pre compiled and pre runned 

and for any help regarding the website you can comment int he comment section.

Post a Comment

0 Comments