Relationship of Pointers with Arrays
The name of the array is the implicit pointer to the first variable of array. For example, for an array arr[5],
arr=&arr[0]
arr+1=&arr[1]
….
Arr+4=&arr[4]
//example code comparing address of array with its implicit pointers
#include
int main () {
int var[] = {10, 100, 200};
for ( int i = 0; i < 3; i++)
{
if((var+i)==&var[i])
printf(“nTRUE”);
}
return 0;
}
//Example code showing how array name can work as pointer
#include
int main () {
int var[] = {10, 100, 200};
for ( int i = 0; i < 3; i++)
{
printf(“Address of var[%d] = %xn”, i, var+i );
printf(“Value of var[%d] = %dn”, i, *(var+i) );
}
return 0;
}
Array of Pointers
It is also possible to create an array of pointers and again each pointer is pointing to individual elements of an array. The following example code giving that scenario.
//example code showing how array of pointers can be created
#include
int main () {
int arr[] = {10, 100, 200};
int *pa[3]; //array of pointers
for ( int i = 0; i < 3; i++)
{
pa[i]=&arr[i];
}
for ( int i = 0; i < 3; i++)
{
printf(“naddress of arr[%d] is : %d”,i,pa[i]);
printf(“nvalue at arr[%d] is : %d”,i,*(pa[i]));
}
return 0;
}