0% found this document useful (0 votes)
14 views

Relation Between Arrays and Pointers

Arrays have fixed addresses for their elements, with each subsequent element having an address incremented by one byte from the previous. Pointers can store any address. In C, array names point to the address of the first element, so references like &arr[0] and arr have the same value and refer to the first element address. Dereference operations like arr[1] and *(arr+1) access the second element. This pointer behavior allows accessing and modifying array elements using pointer notation.

Uploaded by

Facts Trends
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Relation Between Arrays and Pointers

Arrays have fixed addresses for their elements, with each subsequent element having an address incremented by one byte from the previous. Pointers can store any address. In C, array names point to the address of the first element, so references like &arr[0] and arr have the same value and refer to the first element address. Dereference operations like arr[1] and *(arr+1) access the second element. This pointer behavior allows accessing and modifying array elements using pointer notation.

Uploaded by

Facts Trends
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Arrays are closely related to pointers in C programming but the important difference between

them is that, a pointer variable takes different addresses as value whereas, in case of array it
is fixed.

This can be demonstrated by an example:

#include <stdio.h>
int main()
{
char charArr[4];
int i;

for(i = 0; i < 4; ++i)


{
printf("Address of charArr[%d] = %u\n", i, &charArr[i]);
}

return 0;
}
Address of charArr[0] = 28ff44
Address of charArr[1] = 28ff45
Address of charArr[2] = 28ff46
Address of charArr[3] = 28ff47

Note: You may get different address of an array.

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.

Relation between Arrays and Pointers


Consider an array:

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.

&arr[0] is equivalent to arr

Since, the addresses of both are the same, the values of arr and &arr[0] are also the same.

arr[0] is equivalent to *arr (value of an address of the pointer)


Similarly,

&arr[1] is equivalent to (arr + 1) AND, arr[1] is equivalent to *(arr + 1).


&arr[2] is equivalent to (arr + 2) AND, arr[2] is equivalent to *(arr + 2).
&arr[3] is equivalent to (arr + 3) AND, arr[3] is equivalent to *(arr + 3).
.
.
&arr[i] is equivalent to (arr + i) AND, arr[i] is equivalent to *(arr + i).

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));

// *(classes + i) is equivalent to classes[i]


sum += *(classes + i);
}
printf("Sum = %d", sum);
return 0;
}

Output

Enter 6 numbers:
2
3
4
5
3
4
Sum = 21

You might also like