• # Pointers and Arrays

Arrays and pointers are very closely linked together. In most contexts, C++ treats the name of an array as if it were a pointer i.e. memory address of some element. C++ interprets an array name as the address of its first element. That is, if age is an int array to hold 10 integers then age stores the address of age[0], the first element of the array. Consider the following code snippet:

```int *a ;
// a is a pointer to an integer
int age[10] ;
cout<<"Enter the value for array age \n" ;

for(int i=0;i<10;i++)
cin>>age[i] ;
a=age;                  // make a point to the location where age points to
// remember age is a pointer pointing to age[0]
cout<<"\n a points to "<<*a<<"\n";```

In the above code, a is a pointer to an integer and age is an array holding 10 integers. The pointer a is made to point to where age is pointing to. When the data values of these pointers are printed, both values come out to be the same. Thus from the above given code, it is proved that the name of an array is actually a pointer that points to the first element of the array.

Since the name of the array is a pointer to its first element, the arrayname + 1 gives the address of the second element of the array, arrayname + 2 gives the address of the third element and so forth.

Example:

```cout << age[3] ;
//is equivalent to:
cout << *(age+3) ;```

```#include <iostream.h>
void main(void)
{
int *a;         //a is a pointer to integer
int age[10] ;
cout<<"Enter the value for array age \n" ;

for(int i=0;i<10;i++)
{
cout<<i+i<<". " ;
cin>>age[i] ;
}
a=age;          // make a point to the location where age points to
// remember age is a pointer pointing to age[0]
cout<<"\n a points to "<<*a<<"\n";
cout<<"\n age points to "<<*age<<"\n";
}```

Output:

```Enter the value for array age
1. 11
2. 22
3. 33
4. 44
5. 55
6. 66
7. 77
8. 88
9. 99
10. 110

a points to 11

age points to 11```

## Array of pointers

Pointers may also be arrayed like any other data type. To declare an array holding 10 int pointers, the declaration would be as follows:

`int *p[10] ;`

After this declaration, contiguous memory would be allocated for 10 pointers that can point to integers.

Now each of the pointers or the elements of a pointer array can be initialized. To assign the address of an integer variable amount to the forth element of the pointer array, we will write

ip[3] = &amount ;
// Now to find the value of amt, *ip[3] ;

```#include <iostream.h>
void main()
{
int *ip[5];
int f= 65, s= 67, t= 69, fo= 70, fi= 75;
ip[0]= &f;  ip[1]= &s;  ip[2]= &t;
ip[3]= &fo;  ip[4]= &fi;

for(int j=0; i<5; j++)
{
cout<<"The address stored in ip["<< j <<"] poin to "<<ip[i]<<"\n" ;
}
}```

Output:

```The pointer ip[0] points to 65
The pointer ip[1] points to 67
The pointer ip[2] points to 69
The pointer ip[3] points to 70
The pointer ip[4] points to 75
The address stored in ip[0] points to 0x363b241f
The address stored in ip[1] points to 0x1d170f36
The address stored in ip[2] points to 0x1d170f36
The address stored in ip[3] points to 0x1d170f36
The address stored in ip[4] points to 0x1d170f36```
} 240 queries in 0.688 seconds.