Array of Pointers

The way there can be an array of integers or an array of float numbers, similarly, there can be an array of pointers too. Since a pointer contains an address, an array of pointers would be a collection of addresses. For example, a multidimensional array can be expressed in terms of an array of pointers rather than a pointer to a group of contiguous arrays.

A two-dimensional array can be defined as a one-dimensional array of integer pointers by writing:

int *arr[3];

rather than the conventional array definition,

int arr[3][5];

Similarly, an n-dimensional array can be defined as (n-1)-dimensional array of pointers by writing

data-type *arr[subscript 1] [subscript 2]…. [subscript n-1];

The subscript1, subscript2 indicate the maximum number of elements associated with each subscript.

Write a program in which a two-dimensional array is represented as an array of integer pointers to a set of single-dimensional integer arrays.

/* Program calculates the difference of the corresponding elements of two table of integers */
# include <stdio.h>
# include <stdlib.h>
# define MAXROWS 3
void main( )
{
int *ptr1[MAXROWS], *ptr2 [MAXROWS], *ptr3 [MAXROWS];
int rows, cols, i, j;
void inputmat (int *[ ], int, int);
void dispmat (int *[ ], int, int);
void calcdiff (int *[ ], int *[ ], int *[ ], int, int);
printf ("Enter no. of rows & columns \n");
scanf ("%d%d", &rows, &cols);
for (i = 0; i < rows; i++)
{
ptr1[ i ] = (int *) malloc (cols * sizeof (int));
ptr2[ i ] = (int *) malloc (cols * sizeof (int));
ptr3[ i ] = (int *) malloc (cols * sizeof (int));
}
printf ("Enter values in first matrix \n");
inputmat (ptr1, rows, cols);
printf ("Enter values in second matrix \n");
inputmat (ptr2, rows, cols);
calcdiff (ptr1, ptr2, ptr3, rows, cols);
printf ("Display difference of the two matrices \n");
dispmat (ptr3, rows, cols);
}
void inputmat (int *ptr1[MAXROWS], int m, int n)
{
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
scanf ("%d", (*(ptr1 + i) + j));
}
}
return;
}
void dispmat (int *ptr3[ MAXROWS ], int m, int n)
{
int i, j;

for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf ("%d ", *(*(ptr3 + i) + j));
}
printf("\n");
}
return;
}

void calcdiff (int *ptr1[ MAXROWS ], int *ptr2 [ MAXROWS ],
int *ptr3[MAXROWS], int m, int n)
{
int i, j;
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
*(*(ptr3 + i) + j) = *(*(ptr1 + i) + j) - *(*(ptr2 + i) + j);
}
}
return;
}

OUTPUT

Enter no. of rows & columns
3 3

Enter values in first matrix
2 6 3
5 9 3
1 0 2

Enter values in second matrix
3 5 7
2 8 2
1 0 1

Display difference of the two matrices
-1 1 -4
3 1 1
0 0 1

In this program, ptr1, ptr2, ptr3 are each defined as an array of pointers to integers. Each array has a maximum of MAXROWS elements. Since each element of ptr1, ptr2, ptr3 is a pointer, we must provide each pointer with enough memory for each row of integers. This can be done using the library function malloc included in stdlib.h header file as follows:

ptr1[ i ] = (int *) malloc (cols * sizeof (int));

This function reserves a block of memory whose size (in bytes) is equivalent to cols * sizeof(int). Since cols = 3, so 3 * 2 (size of int data type) i.e., 6 is allocated to each ptr1[ 1 ], ptr1[ 2 ] and ptr1[ 3 ]. This malloc function returns a pointer of type void. This means that we can assign it to any type of pointer. In this case, the pointer is type-casted to an integer type and assigned to the pointer ptr1[ 1 ], ptr1[ 2 ] and ptr1[ 3 ]. Now, each of ptr1[ 1 ], ptr1[ 2 ] and ptr1[ 3 ] points to the first byte of the memory allocated to the corresponding set of one-dimensional integer arrays of the original two-dimensional array.

The process of calculating and allocating memory at run time is known as dynamic memory allocation. The library routine malloc can be used for this purpose.

Instead of using conventional array notation, pointer notation has been used for accessing the address and value of corresponding array elements which has been explained to you in the previous section. The difference of the array elements within the function calcdiff is written as

*(*(ptr3 + i) + j) = *(*(ptr1 + i) + j) – *(*(ptr2 + i) + j);

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: