Friday, Jun 23rd

Last update12:59:40 PM GMT

Pointers and 2-D arrays: A Thumb Rule

Write e-mail

How pointers and arrays gel with each other has already been explained rather beautifully in the articles on Pointers: Basics , Pointers:The Array Relation and  Pointers: complicated stuff made easy !!

argaiv1352

Without taking any credit away from Snoopy :), this is just a small tut on accessing 2D arrays with pointers. Why a separate one? Well, how snoopy learned pointers in college, is different from how I did…so let’s put both the approaches and let you decide which suits you best :)

Let’s consider a 2-D array

N[3][3] = {2,4,3,6,8,5,9,7,1}; (the address of each element is in white)

Pointers and 2D arrays

Better represented as

n[3][3] = {{2,4,3},

                {6,8,5},

                {9,7,1}};

 And even better represented as in the adjoining figure:

Pointers and 2D arrays

Now to explain how pointers help access the elements of the array and their addresses, let’s play with some arrows and stars :)

The first thing:

n refers to the starting address of the array, i.e. 100

Now look at this figure

Pointers and 2D arrays

 

This is the notation we’ll now use and try to see how to use the * to access the []

 We have 3 types of boxes. 

  • The inner one contains the element itself
  • The 2nd one contains the each row.
  • The 3rd box houses the entire set of rows in that dimension.(We’ll see how 2 3D array works with this approach as well)

Now forget the outermost box and as it is a 2 D array, let’s only focus on the 2 inner boxes.

n, as usual, points to the first row. *n however, points to the first element of the first row. (Note the difference).

So, now if we do a (n+1), we’ll automatically be taken to the second row. i.e. n will now point to the row containing {6,8,5} and its value would be 112.

In contrast, if we do a (*n+1), we’ll now be taken to the element 4, and n will then be pointing to the element 4 and will have its address, i.e. 104.

So the thumb rule is: whenever you apply a *, you move inside a box.

How to now access the element inside the boxes? Simple, apply the *.

Pointers and 2D Arrays

 

 

For (*n+1), we now point to 4. To access it, make it *(*n+1). The value returned will now be 4.

The other scenario (n+1). Now, *(n+1) will contain 6’s address, right? (As (n+1) contained the row’s starting address (which happened to be same as 6’s)). To access 6, use **(n+1). Simple!


Let’s access the element 7 now.

First we need to move 2 rows. So we do (n+2).

Now we are on the row {9,7,1}.

Next we need to move to 7. So first we point ourselves to the element containing 9 i.e. *(n+2)

Now we add 1 to it to point to 7 - (*(n+2)+1). To access 7, add a * i.e. *(*(n+2)+1)

And as we have already seen that *(n+2) is nothing but n[2], this above notation is the same as saying n[2][1].

 

Try accessing 3D arrays now.

n[2][3][3] = {

                    {{2,4,3},

                      {6,8,5},

                      {9,7,1}},

                    {{12,14,13},

                      {16,18,15},

                      {19,17,11}},

                    };

This means there are two 2D arrays of dimensions 3x3.

So make 3 boxes (actually 4, but as we did before, ignore the 4th one). Use the same notation as we did and see if it works.

Pointers and 2D arrays

Share this post



Add comment

Please refrain from using slang or abusive language in the comments.
To avoid waiting for your comment to be published after being reviewed, please log in as a registered user.


Security code
Refresh

Web Hosting