# 14.12 – Pointers and Arrays

by on January 25, 2018

## Pointers and arrays

Pointers and arrays are closely related. So far we know that, an array is a set of elements of the same type stored in adjacent memory locations, total set is identified with its name and every element is accessed with its index. Say for example, if the name of array is a then a[i] access the ith element in the set. In C language index of the first element is 0 and the index of the last element is n-1 where n is the size of array.

Now we will see the inner story of array. When an array is declared then the compiler allocates the set of elements in adjacent memory locations and the name of array gives the address of base element or we can say the address of array.

```short a[5];
```

```#include<stdio.h>
int main()
{
short a[5];
printf("%u",a);
printf("\n%u",&a[0]);
return 0;
}
```

Output:
65516
65516

With the above program output, it is confirmed that the name of array gives the address of base element.

### Dereferencing array:

Because the name of array a gives the address of base element, we can access the first element using dereferencing as *a or *(a+0)
As the scale factor of short is two, a+1 (65518) refers the next element to the base element, So we can access the second element as *(a+1)
The same way third element can be accessed as *(a+2) and fourth can be accesses as *(a+3) and so on ..

So, we can access array elements either by using index as a[i] or through dereferencing *(a+i)
Accessing array elements through dereferencing is faster than accessing through index.

```#include<stdio.h>
int main()
{
short a[]={34,56,67,22,34,76};
short i;
printf("Elements of array:\n");
/*for(i=0;i<6;i++)
printf("%5d",a[i]);*/
for(i=0;i<6;i++)
printf("%5d",*(a+i));   /* using *(a+i) in place of a[i]  */
return 0;
}
```

Output:
Elements of array:
34   56   67   22   34   76

### Reading the data through dereferencing:

We generally read the data from the keyboard using sacnf() by specifying the address (location) of memory allocation (identifier).
We write &a[0] to read the data into the first element of array, the same thing can be written as a or (a+0).

```scanf("%d",&a[0]);
```

or

```scanf("%d",a+i);
```

We write &a[1] to read the data into the second element of array, the same thing can be written as (a+1)

```scanf("%d",&a[1]);
```

or

```scanf("%d",a+1);
```

To read the data from the keyboard and store into the array element by element we can write in either of the ways

```for(i=0;i<n;i++)
scanf("%d",&a[i]);
```

or

```for(i=0;i<n;i++)
scanf("%d",a+i);
```

Specification: Accept n elements into an array and print the elements in the accepted order

```#include<stdio.h>
int main()
{
short a[50],n,i;
printf("How many elements?");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
scanf("%d",a+i);      /* writing a+i instead of &a[i]   */
printf("The given elements are:\n");
for(i=0;i<n;i++)
printf("%5d",*(a+i)); /* writing *(a+i) instead of a[i] */
return 0;
}
```

Execution:
How many elements?5
Enter 5 elements:
45 67 45 33 23
The given elements are:
45   67   45   33   23

Specification 2: Accept n elements into an array and print the sum of all the elements

```#include<stdio.h>
int main()
{
short a[50],n,i,sum;
printf("How many elements?");
scanf("%d",&n);
printf("Enter %d elements:\n",n);
for(i=0;i<n;i++)
scanf("%d",a+i);      /* writing a+i instead of &a[i]   */
for(sum=0,i=0;i<n;i++)
sum=sum+*(a+i);       /* writing *(a+i) instead of a[i] */
printf("Sum of all the elements %d",sum);
return 0;
}
```

Execution:
How many elements?5
Enter 5 elements:
12 56 43 45 67
Sum of all the elements 223

### Pointer arithmetic can’t be done with array name:

Though the name of array gives the address of array it is not a pointer hence array name can’t be incremented or decremented.

```short x[10];
x++;
```

It results error as we can’t increment the location at which array is existed.

Conclusion:

• The name of array gives the address of array or the address of base element.
• Every element of array can be accessed either by using index a[i] or by using dereferencing *(a+i)
• It is faster to access the elements through dereferencing *(a+i)

Previous post: