¤ Home » Programming » C Tutorial » Pointers in C - Part 4 of 9

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


Share:




comments powered by Disqus


Download Mobile App for Android


Web Service Offers

.CLUB Registration
1,199 199 (83.4% off)
per Year

Ecommerce on the Cloud

An online store hosted on the Cloud. Sell unlimited products. Integrated business functions. Well-suited for high traffic website.

39,999 29,999 (25% off)

Ecommerce Premium

An e-commerce website well-suited for companies having a large portfolio of products. Sell unlimited products. Manage both offline and online sales from one system.

40,000 25,000 (37.5% off)

Ecommerce Business

Launch your online business. Sell up to 5000 products. Collect payment via multiple online and offline payment methods.

35,000 25,000 (28.57% off)

Ecommerce Pro

Get a Mobile friendly online store front. Sell up to 1000 products.

35,000 25,000 (28.57% off)

Ecommerce Starter

Start selling your products online. Scale up as your business grows.

35,000 25,000 (28.57% off)

Website on the Cloud

Custom built, responsive corporate website hosted on the cloud. Mirrored on 3 servers. Includes visitor signup with CRM functions/

19,999 9,999 (50% off)

Website Corporate

Custom built, responsive corporate website with CRM. Our most popular website plan.

19,000 9,000 (52.63% off)

Website Business - 1000 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

18,000 8,000 (55.56% off)

Website Business - 500 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

17,500 7,500 (57.14% off)

Website Business - 100 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

17,000 7,000 (58.82% off)

Website Business - 50 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

16,500 6,500 (60.61% off)

Website Business - 25 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

16,000 6,000 (62.5% off)

Website Starter - 5 Pages

Get started with your own website at most affordable cost.

15,000 5,000 (66.67% off)


Looking to build a website?
Launch a 5 page website in 1 day at only Rs.499/year. Hosting included.





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 XLRI, industry professionals, and govt. officials.

Rajeev has founded Computer Solutions & WebServicesWorldwide.com, and has hands-on experience of building variety of web applications and portals, that include - SAAS based ERP & e-commerce systems, independent B2B, B2C, Matrimonial & Job portals, and many more.



Copyright © How2Lab.com. All rights reserved.

Refer a friend | Sitemap | Disclaimer | Privacy Policy