# POINTERS AND ARRAYS

An array is a collection of similar data type elements. When we declare an array then consecutive memory locations are allowed to the array elements. The base address of 0th element of an array.
The name of the array also gives the base address of the array.
EX: int a  = {5, 10, 15, 20};

Here a  means that ‘a’ has 4 elements and is of ‘int’ data type.
Following are the two main points to understand the concept of pointers with arrays.

1. Elements of array are stored in the consecutive memory locations.
2. When an array pointer is incremented it refers to the next location of its data type.
Consider the same example:
int a  = {5, 10, 15, 20};
int *b;
b=a;
This shows ‘b’ is a pointers variable which holds the base address of the array ‘a’.
(Or)
We can say b=&a;

When pointer variable is incremented by ‘1’ then it contains the base address +2 because pointer is of type ‘int’. The address of the next element will be also base address +2 because element of array are stored in consecutive memory location. So, we can say when an array pointer is incremented it refers to the next immediate location of the array. Hence
b=&a ;
b+1=&a;
b+2=&a;
b+3=&a;

we know that the name of the array ‘a’ is the base address of the array. This can also be written as a+0 i.e., &a<---> a+0, which gives the base address of the array. To get the value at that address, we can use *a (or) *(a+0) (or) a.
i.e., *a gives the first element of the array.

*(a+1) gives the first element of the array.
*(a+2) gives the second element of the array.
*(a+i) gives the ith element of the array.
This can also be written as *(i+a) (or) i[a].

PROGRAM: To print the value of the array element.

```Void main ()
{
int a={5,10,15,20};
int i;
clrscr();
for(i=0;i<4;i++)
{
printf("%d....",a[i]);
printf("%d..............",*(a+i));
printf("%d..............",*(i+a));
printf("%d \n",i[a]);
}
getch();
}

```

O/P:
5 .........5 .........5 ..........5
10 ........10 ........10 ........10
15 ........15 ........15 ........15
20 ........20 ........20 ........20

PROGRAM: To accept 10 numbers and print the total with use of pointer.

```Void main();
{
int i, sum=0;
int a,*p;
clrscr();
printf("enter 10 elements into array\n");
for(i=0;i<10;i++)
{
Scanf("%d",&a[i]);
}
printf("array elements are \n");
for(i=0;i<10;i++)
{
printf("%d \t",a[i]);
}
P=&a;
For(i=0;i<10;i++)
{
Sum=sum+ *p;
P=p+1;
}
printf("sum of 10 numbers=%d",sum);
getch();
}
```

O/P:
Enter 10 elements into array:
1 2 3 4 5 6 7 8 9 10.
Array elements are:
1 2 3 4 5 6 7 8 9 10
Sum of 10 numbers=55.
PROGRAM: Use the pointer with function to print the value and address of the array element.

```Void main()
{
Void fun(int *);
int a={1,2,3,4};
clrscr();
fun(a);
getch();
}

Void  fun(int *b)
{
int i;
for(i=0;i<4;i++)
{
Printf("value of a[%d] is %d \n",i,*b);
Printf("address of a[%d] is %d \n",i,b);
b=b+1;     /*incrementing ‘b’ by 1 to get the address of next array element */
}
}

```

O/P:
Value of a is 1
Address of a is 1000
Value of a is 2
Address of a is 1002
Value of a is 3
Address of a is 1004
Value of a is 4
Address of a is 1006.

PROGRAM: To accept 10 numbers and sort them with the use of pointers.

```Void main()
{
int a,i,j,t;
int *p;
clrscr();
printf("enter the array elements");
for(i=0;i<10;i++)
{
scanf("%d",&a[i]);
}
printf("array elements before sorting \n");
for(i=0;i<10;i++)
{
printf("%d \t",a[i]);
}
b=&a;
for(i=0;i<10;i++)
{
for(j=i+1;j<10;j++)
{
if(*(b+i)>*(b+j))
{
t=*(b+i);
*(b+i)=*(b+j);
*(b+j)=t;
}
}
}
b=&a
printf("elements after sorting are \n");
for(i=0;i<10;i++)
{
printf("%d \t",*b);
b=b+1;
}
getch();
}

```

O/P:
Enter array elements
7 5 3 8 1 4 2 9 11 10
Elements before sorting are:
7 5 3 8 1 4 2 9 11 10
Elements after sorting are:
1 2 3 4 5 7 8 9 10 11

### POINTERS WITH MULTIDIMENTIONAL ARRAY

Two dimensional arrays are also called a matrix. Here first subscript is a row number and second subscript is column numbers.
EX:
a;
We know that ‘a’ gives the base address of the 2-D array. This can also be written as a. This gives the 0th element of the 2-D array.
But the 0th element of 2-D array is a 1-dimensional array.
Hence a gives the base address of 0th 1-D array.
a gives the base address of 1st 1-D array.
a gives the base address of the 2nd 1-D array.
a[i] gives the base address of the (i)th 1-D array.
We know that a=*(a+0)
a=*(a+1)
a[i]=*(a+i)

Copyright © 2018-2020 TutorialToUs. All rights reserved.