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 1:


One way of dealing with the problem is through the use of the typedef keyword. To allocate a 2 dimensional array of integers recall that the following two notations result in the same object code being generated:


       multi[row][col] = 1;            *(*(multi + row) + col) = 1;
It is also true that the following two notations generate the same code:
       multi[row]                   *(multi + row)


Since the one on the right must evaluate to a pointer, the array notation on the left must also evaluate to a pointer. In fact multi[0] will return a pointer to the first integer in the first row, multi[1] a pointer to the first integer of the second row, etc. Actually, multi[n] evaluates to a pointer to that array of integers that make up the n-th row of our 2  dimensional array. That is, multi can be thought of as an array of arrays and multi[n] as a pointer to the n-th array of this array of arrays. Here the word pointer is being used to represent an address value. While such usage is common in the literature, when reading such statements one must be careful to distinguish between the constant address of an
array and a variable pointer which is a data object in itself.


Consider now:
--------------- Program 9.1 --------------------------------
/* Program 9.1 from PTRTUT10.HTM              6/13/97 */
#include <stdio.h>
#include <stdlib.h>
#define COLS 5
typedef int RowArray[COLS];
RowArray *rptr;
int main(void)
{
       int nrows = 10;
       int row, col;
       rptr = malloc(nrows * COLS * sizeof(int));
       for (row = 0; row < nrows; row++)
      {
            for (col = 0; col < COLS; col++)
           {
                 rptr[row][col] = 17;
           }
      }
      return 0;
}
------------- End of Prog. 9.1 --------------------------------


Here I have assumed an ANSI compiler so a cast on the void pointer returned by malloc() is not required. If you are using an older K&R compiler you will have to cast using: 


      rptr = (RowArray *)malloc(.... etc.


Using this approach, rptr has all the characteristics of an array name name, (except that rptr is modifiable), and array notation may be used throughout the rest of the program. That also means that if you intend to write a function to modify the array contents, you must use COLS as a part of the formal parameter in that function, just as we did when
discussing the passing of two dimensional arrays to a function.



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