Arrays in C (2D Array) – Part 6

As I told you earlier the concept of arrays is very closely related to pointers. Arrays will work faster by using pointers rather than using a subscript. So it is always recommended to access arrays by using pointers. In the last tutorial I gave a brief overview on 2D arrays in C. Today I will discuss the usage of 2D arrays with pointers. This is one of the most complex topics of C programming, so I would like to suggest you to read this tutorial with full concentration.

2D Arrays with Pointers

A 2D array is nothing but a combination of 1D arrays. To prove my point I would like to show you an example.

Output

2D Arrays with Pointers

Explanation

  • In the beginning I have declared a 2D array with 4 rows and 2 columns and also initialized it. So we can say that this 2D array is a collection of four 1D arrays having 2 elements each.
  • After that I have declared integer variable x which will act as a loop counter.
  • Now I have started the for loop with one printf() function. Consider carefully the arguments in printf() function. I have given two arguments which are x and s[x].
  • As you can see I am only accessing the 2D array with one dimension. So it will give the addresses of only 1D arrays. s[0] will show the address of first element of first 1D array, s[1] will show address of first element of second 1D array and so on.

Access 2D Array Using Pointer Notation

The best way to learn it is through a program.


Output

Access 2D Array Using Pointer Notation

Leave a Reply

Your email address will not be published. Required fields are marked *