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

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 3 PDF Print E-mail
Written by Hemanshu   
Saturday, 29 December 2007
Article Index
A TUTORIAL ON POINTERS AND ARRAYS IN C - Part 3
Page 2
Page 3
Page 4
Page 5

METHOD 3:


Consider the case where we do not know the number of elements in each row at compile time, i.e. both the number of rows and number of columns must be determined at run time. One way of doing this would be to create an array of pointers to type int and then allocate space for each row and point these pointers at each row. Consider:


-------------- Program 9.2 ------------------------------------
/* Program 9.2 from PTRTUT10.HTM              6/13/97 */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
      int nrows = 5;          /* Both nrows and ncols could be evaluated */
      int ncols = 10;         /* or read in at run time */
      int row;
      int **rowptr;
      rowptr = malloc(nrows * sizeof(int *));
      if (rowptr == NULL)
      {
           puts("\nFailure to allocate room for row pointers.\n");
           exit(0);
      }
      printf("\n\n\nIndex         Pointer(hex)        Pointer(dec)       Diff.(dec)");
      for (row = 0; row < nrows; row++)
      {
           rowptr[row] = malloc(ncols * sizeof(int));
           if (rowptr[row] == NULL)
           {
                printf("\nFailure to allocate for row[%d]\n",row);
                exit(0);
           }
           printf("\n%d               %p            %d", row, rowptr[row],
rowptr[row]);
           if (row > 0)
           printf("                     %d",(int)(rowptr[row] - rowptr[row-1]));
      }
      return 0;
}
--------------- End 9.2 ------------------------------------


In the above code rowptr is a pointer to pointer to type int. In this case it points to the first element of an array of pointers to type int. Consider the number of calls to malloc():


      To get the array of pointers                        1       call
      To get space for the rows                           5       calls
                                                       -----
                           Total                                          6       calls


If you choose to use this approach note that while you can use the array notation to access individual elements of the array, e.g. rowptr[row][col] = 17;, it does not mean that the data in the "two dimensional array" is contiguous in memory.
You can, however, use the array notation just as if it were a continuous block of memory. For example, you can write:


      rowptr[row][col] = 176;


just as if rowptr were the name of a two dimensional array created at compile time. Of course row and col must be within the bounds of the array you have created, just as with an array created at compile time. If you want to have a contiguous block of memory dedicated to the storage of the elements in the array you can do it as follows:



Last Updated ( Saturday, 29 December 2007 )
 
< 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
Powered password keylogger is a driver-based software keylogger by Eltima

Tired of MS Office ? Try Ashampoo Office 2008 . All OS supported