Pointer
to arrays
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();
}
0 Comments