How2Lab Logo
tech guide & how tos..

Pointers in C - Part 4 of 9

A two-dimensional array can be treated as a collection of rows where each row can be interpreted as a one-dimensional array. Thus, a two dimensional array can be visualized as a pointer to a group of contiguous one-dimensional arrays. Such declarations can be done as follows:

<datatype> (*<pointer variable>) [<expression>]

For example, the array declaration:int x[10][20]; can be written as:int (*x)[20];.

This declaration defines a two dimensional array of unknown number of rows, where each row is a one-dimensional array consisting of 20 contiguous elements. In this declaration x is a pointer to a pointer that typically stores the address of the pointer pointing the first row or the first string in the group of strings.

Let us consider the following example:

int x[3][3] = {
		{1, 2, 3},
		{4, 5, 6},
		{7, 8, 9}

A schematic representation of the array x is shown in the following figure:

To print the value of an element say x[1][1] of the array x using the conventional subscript notation, one can use the following printf statement:

printf("%d", x[1][1]);

Accessing each element of a two-dimensional array can also be done using a pointer instead of using subscripts. In order to refer to the 2nd element in the 2nd row using the pointer x, first we have to access row 2, which is done by adding 1 to the address value of x i.e., (x + 1). But (x + 1) is the address location of the pointer that points to row 2. Hence prefixing (x + 1) by indirection operator (*) will extract the address of the first element of row 2 i.e., *(x + 1), is equal to &x[1][0]. The address of the next element is calculated by adding one to the previous element's address i.e., *(x + 1) + 1. To find out the content of that location we again prefix the address by the indirection operator *. So *(*(x + 1) + 1) will correspond to x[1][1] and the following printf statement can be used to print it.

printf("%d", *(*(x + 1)+ 1));

Thus, *(*(x + row) + col) actually corresponds to the element x[row][col].

To generalize what we have understood so far, int (*x)[ncols] is a pointer to an array with undefined number of rows where each row consists of a contiguous block of ncols elements. To create the two dimensional array, memory space has to be allocated for all the rows of the array. This can be done using the malloc function which would allocate space for the specified number of rows (nrows) and columns (ncols), as illustrated below:

*x = (int *)malloc(nrows * ncols * sizeof(int));

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 © All rights reserved.