Relationship between Pointer and Arrays in C
Now we can relate the concept of pointer arithmetic to understand relationship between pointer and arrays in C:
- Arrays elements are always stored in contiguous locations
- A pointer when incremented always points to immediate next location of its type
- A pointer when decremented always points to an immediate previous location of its type.
A C program that prints out elements and its memory locations in given array:
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50, 60, 70, 80}, i;
for (i = 0; i < 8;i++)
{
printf("Element: %d Location: %u\n", arr[i], &arr[i]);
}
return 0;
}
Output:
Element: 10 Location: 1842722032
Element: 20 Location: 1842722036
Element: 30 Location: 1842722040
Element: 40 Location: 1842722044
Element: 50 Location: 1842722048
Element: 60 Location: 1842722052
Element: 70 Location: 1842722056
Element: 80 Location: 1842722060
The above output clearly shows contiguous memory locations are reserved for the given array starting from 1842722032 to 1842722060 each separated by 4 bytes[which is the size of integer values].
Now using the concept of pointer arithmetic, let us write a program to access the array elements using pointer:
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50, 60, 70, 80}, i,*ptr;
ptr = &arr[0]; // assign the first address of array to pointer variable
for (i = 0; i < 8; i++)
{
printf("Element: %d Location: %u\n", *ptr, ptr);
ptr++; // increment pointer to points to next location
}
return 0;
}
Output:
Element: 10 Location: 1834824944
Element: 20 Location: 1834824948
Element: 30 Location: 1834824952
Element: 40 Location: 1834824956
Element: 50 Location: 1834824960
Element: 60 Location: 1834824964
Element: 70 Location: 1834824968
Element: 80 Location: 1834824972
More about Pointers and Array
Let us consider following array:
int arr[] = {10,20,30,40,50};
In any given array, the first address of the array which is &arr[0] is also called the base address of the array. We can get the base address of the array by just mentioning the array name i.e arr or &arr[0].
C program to understand the concept of base address of the array
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50, 60, 70, 80}, i,*ptr;
ptr = &arr[0];
printf("The base address of array is: %u\n", ptr);
printf("The base address of array is: %u\n", arr); // arr and ptr points to base address
return 0;
}
Output:
The base address of array is: 1828877552
The base address of array is: 1828877552
Now by using above concepts we can access the array elements using its base address as follows:
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50, 60, 70, 80}, i;
for (i = 0; i < 8; i++)
{
printf("Element: %d Location: %u\n", *(arr+i),arr+i);
}
return 0;
}
Output:
Element: 10 Location: 1870427376
Element: 20 Location: 1870427380
Element: 30 Location: 1870427384
Element: 40 Location: 1870427388
Element: 50 Location: 1870427392
Element: 60 Location: 1870427396
Element: 70 Location: 1870427400
Pointer and 2D array
A 2D array is an array of arrays. Basically, each row in a 2D array is a one 1D dimensional array.Thus, in the following declaration:
int arr[3][2];
It consists of 3 one-dimensional arrays each with 2 elements. Now we can refer to the base address of each 3 one-dimensional arrays as: arr[0], arr[1], arr[2]. Let us write a C program to print base addresses of each 3 array:
#include <stdio.h>
int main() {
int arr[3][2] = {10, 20, 30, 40, 50, 60}, i;
for (i = 0; i < 3; i++)
{
printf("Array : %d Location: %u\n",i,arr[i]);
}
return 0;
}
Output:
Array : 0 Location: 1861661952
Array : 1 Location: 1861661960
Array : 2 Location: 1861661968
The output clearly shows each address is separated by 8 units which is for 2 elements in each row X 4 bytes each.
Using above concepts a 2D array can be accessed using pointer by following expressions:
arr[i][j]
*(arr[i]+j)
*(*(arr+i)+j)
Write a C Program to demonstrate the relationship between pointer and 2D array
#include <stdio.h>
int main() {
int arr[3][2] = {10, 20, 30, 40, 50, 60}, i,j;
for (i = 0; i < 3; i++)
{
for (j = 0; j < 2;j++)
{
printf("Element : %d Location: %u\n",*(*(arr+i)+j),*(arr+i)+j);
}
}
return 0;
}
Output
Element : 10 Location: 1863742720
Element : 20 Location: 1863742724
Element : 30 Location: 1863742728
Element : 40 Location: 1863742732
Element : 50 Location: 1863742736
Element : 60 Location: 1863742740