How2Lab Logo
tech guide & how tos..


Pointers in C - Part 5 of 9


A two dimensional array can be treated as a one dimensional array of pointers, i.e. a one dimensional array of addresses, where each element of the pointer array points to a one dimensional array of elements. Each one dimensional array can be created by dynamic memory allocation and its base addresses assigned to one of the pointers in the 1-dimensional array of pointers. The syntax used for declaring an array of pointers is:

<datatype> *<array_name>[<expression>];

The advantage associated with this approach, as compared to the conventional 2-dimension array declaration, is that a fixed block of memory need not be reserved in advance. Dynamic allocation of memory is carried out using the malloc function.

Consider the following declaration:

int *x[5];

which says x is an array of 5 pointers. Each of these pointers can be used to create a one dimensional array as explained above. The difference in the declaration between pointer to an array and array of pointers can be seen from the missing braces ( ) around *x.

To allocate adequate memory for the array, you can use a for loop construct so that in each pass space for a one dimensional array having a given number of elements (ncols) is created using the malloc function. See illustration below:

/* Demonstration of memory allocation for 2-dimensional array */ 
#include <stdio.h>
#include <malloc.h>
main()
{
	int i, *x[5]; 
	for(i=0; i < 5; i++) 
		x[i] = (int *)malloc(ncols * sizeof(int));
	...
} 

Here is a realistic program to demonstrate usage of array of pointers to create two dimensional matrices and perform matrix multiplication.

#include <stdio.h>
#include <malloc.h>
main()
{
	int *a[10], *b[10], *c[10]; 
	int i, r1, r2, c1, c2;
	void getMatrixElements();
	void displayMatrix();
	void multiplyMatrix(); 

	printf("Size of Matrix A (r1, c1)?: ");
	scanf("%d,%d",&r1,&c1);

  	printf("Size of Matrix B (r2, c2)?: ");
   	scanf("%d,%d",&r2,&c2);
 
	if(c1 != r2)
	{ 
	  printf("\n\nError! Matrix multiplication not possible.\n"); 
	  printf("No. of cols in matrix A should be equal to no. of rows in matrix B\n");
	  exit();
	}

	/* Memory allocation */
	for(i=0; i < r1; i++) 
	{
		a[i] = (int *)malloc(c1 * sizeof(int));
		c[i] = (int *)malloc(c2 * sizeof(int));
	}

	for(i=0; i < r2; i++) 
		b[i] = (int *)malloc(c2 * sizeof(int));

	printf("Matrix A elements?\n");
	getMatrixElements(a, r1, c1);
	displayMatrix(a, r1, c1);

	printf("\nMatrix B elements?\n"); 
	getMatrixElements(b, r2, c2); 
	displayMatrix(b, r2, c2); 

	multiplyMatrix(a, b, c, r1, c1, c2); 
	printf("\nResultant matrix C -(%d X %d)\n", r1, c2);
	displayMatrix(c, r1, c2);
}

void getMatrixElements(int *x[10], int r, int c)
{
	int i, j; 
	for(i=0; i < r; i++)
	{ 
		for(j=0; j < c; j++)
		{
			scanf("%d",(*(x + i) + j)); fflush(stdin); 
			printf("\t");
		}
		printf("\n"); 
	}
} 

void displayMatrix(int *x[10], int r, int c) 
{ 
	int i, j; 
	for(i=0; i < r; i++)
	{ 
		for(j=0; j < c; j++)
		{
			printf("%d\t",*(*(x + i) + j)); 
		}
		printf("\n");
	}
}

void multiplyMatrix(int *a[10],int *b[10],int *c[10],int rw,int m,int cl)
{ 
	int i, j, k; 
	for(i=0; i < rw; i++)
	{
		for(j=0; j < cl; j++)
		{ 
			*(*(c + i) + j) = 0; 
			for(k=0; k < m; k++) 
			  *(*(c + i) + j) += (*(*(a + i) + k)) * (*(*(b + k) + j)); 
		}
	}
}



Exercise

Distinguish between the following:

 i) int *a[10];
ii) int (*a)[10];


Share:
Buy Domain & Hosting from a trusted company
Web Services Worldwide | Hostinger
About the Author
Rajeev Kumar
CEO, Computer Solutions
Jamshedpur, India

Rajeev Kumar is the primary author of How2Lab. He is a B.Tech. from IIT Kanpur with several years of experience in IT education and Software development. He has taught a wide spectrum of people including fresh young talents, students of premier engineering colleges & management institutes, and IT professionals.

Rajeev has founded Computer Solutions & Web Services Worldwide. He has hands-on experience of building variety of websites and business applications, that include - SaaS based erp & e-commerce systems, and cloud deployed operations management software for health-care, manufacturing and other industries.


Refer a friendSitemapDisclaimerPrivacy
Copyright © How2Lab.com. All rights reserved.