Home arrow C programming arrow A TUTORIAL ON POINTERS AND ARRAYS IN C - Part 2

Language Translator

Hacking Zone

Hacking Tools
Attacking

Configure Windows

Windows Configuration

Novels

Mix Novels

Human Personality

Body Language
A TUTORIAL ON POINTERS AND ARRAYS IN C - Part 2 PDF Print E-mail
Written by Hemanshu Patel   
Saturday, 29 December 2007
Article Index
A TUTORIAL ON POINTERS AND ARRAYS IN C - Part 2
Page 2
Page 3
Page 4
Page 5
Page 6

Now, the compiler knows how many columns are present in the array so it can interpret multi + 1 as the address of the ' in the 2nd row above. That is, it adds 10, the number of columns, to get this location. If we were dealing with integers and an array with the same dimension the compiler would add 10*sizeof(int) which, on my machine, would be 20.
Thus, the address of the 9 in the 4th row above would be &multi[3][0] or *(multi + 3) in pointer notation. To get to the content of the 2nd element in the 4th row we add 1 to this address and dereference the result as in


     *(*(multi + 3) + 1)


With a little thought we can see that:


     *(*(multi + row) + col)             and

     multi[row][col]                     yield the same results.


The following program illustrates this using integer arrays instead of character arrays.


------------------- program 6.1 ----------------------
/* Program 6.1 from PTRTUT10.HTM              6/13/97*/
#include <stdio.h>
#define ROWS 5
#define COLS 10
int multi[ROWS][COLS];
int main(void)
{
     int row, col;
     for (row = 0; row < ROWS; row++)
     {
           for (col = 0; col < COLS; col++)
           {
                 multi[row][col] = row*col;
           }
      }
      for (row = 0; row < ROWS; row++)
      {
           for (col = 0; col < COLS; col++)
           {
                printf("\n%d ",multi[row][col]);
                printf("%d ",*(*(multi + row) + col));
           }
      }
      return 0;
}
----------------- end of program 6.1 ---------------------


Because of the double de-referencing required in the pointer version, the name of a 2 dimensional array is often said to be equivalent to a pointer to a pointer. With a three dimensional array we would be dealing with an array of arrays of arrays and some might say its name would be equivalent to a pointer to a pointer to a pointer. However, here we
have initially set aside the block of memory for the array by defining it using array notation. Hence, we are dealing with a constant, not a variable. That is we are talking about a fixed address not a variable pointer. The dereferencing function used above permits us to access any element in the array of arrays without the need of changing the value of that address (the address of multi[0][0] as given by the symbol multi).


Last Updated ( Monday, 06 October 2008 )
 
< Prev   Next >
Your Ad Here

Donate us!!

Enter Amount:

RSS socialnet

Add to MyYahoo!
Subscribe in NewsGator Online
Add to Newsburst
Add to Google
Add to My AOL
Add to Pluck
Subscribe in FeedLounge
Add to Windows Live
Add to NetVibes
Subscribe in Rojo
Subscribe in Bloglines
Add to MyMSN
Add to Plusmo for your cellphone
Add to PageFlakes
Add to Technorati
Add to BlinkBits