Relation Between Arrays and Pointers
Relation Between Arrays and Pointers
them is that, a pointer variable takes different addresses as value whereas, in case of array it
is fixed.
#include <stdio.h>
int main()
{
char charArr[4];
int i;
return 0;
}
Address of charArr[0] = 28ff44
Address of charArr[1] = 28ff45
Address of charArr[2] = 28ff46
Address of charArr[3] = 28ff47
Notice, that there is an equal difference (difference of 1 byte) between any two consecutive
elements of array charArr.
But, since pointers just point at the location of another variable, it can store any address.
int arr[4];
In C programming, name of the array always points to address of the first element of an
array.
In the above example, arr and &arr[0] points to the address of the first element.
Since, the addresses of both are the same, the values of arr and &arr[0] are also the same.
In C, you can declare an array and can use pointer to alter the data of an array.
Example: Program to find the sum of six numbers with arrays and pointers
#include <stdio.h>
int main()
{
int i, classes[6],sum = 0;
printf("Enter 6 numbers:\n");
for(i = 0; i < 6; ++i)
{
// (classes + i) is equivalent to &classes[i]
scanf("%d",(classes + i));
Output
Enter 6 numbers:
2
3
4
5
3
4
Sum = 21