Thread: Another Pointer Q

  1. #1
    Registered User
    Join Date
    Jul 2003
    Posts
    28

    Another Pointer Q

    Ive been reading through a tutorial on pointer I found on here and have understood it so far. Im now looking at a sample program that creates a 2 dimensional array at run time. I can understand it but there a bit of code there that I dont understand why is there. Ive tried experimenting and even removing this piece of code and it seems to make no difference. The
    code im confused with is the sections that starts

    /* next we allocate room for the pointers to the rows */

    can anyone explain why this is needed?


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void)
    {
        int **rptr;
        int *aptr;
        int *testptr;
        int k;
        int nrows = 5;     /* Both nrows and ncols could be evaluated */
        int ncols = 8;    /* or read in at run time */
        int row, col;
    
    
        /* we now allocate the memory for the array */
        aptr = malloc(nrows * ncols * sizeof(int));
        if (aptr == NULL)
        {
            puts("\nFailure to allocate room for the array");
            exit(0);
        }
    
    
        /* next we allocate room for the pointers to the rows */
        rptr = malloc(nrows * sizeof(int *));
        if (rptr == NULL)
        {
            puts("\nFailure to allocate room for pointers");
            exit(0);
        }
    
    
        /* and now we 'point' the pointers */
        for (k = 0; k < nrows; k++)
        {
            rptr[k] = aptr + (k * ncols);
        }
    
    
        printf("\n\nIllustrating how row pointers are incremented");
        printf("\n\nIndex   Pointer(hex)  Diff.(dec)");
        for (row = 0; row < nrows; row++)
        {
            printf("\n%d         %p", row, rptr[row]);
            if (row > 0)
            printf("              %d",(rptr[row] - rptr[row-1]));
        }
    
    
        printf("\n\nAnd now we print out the array\n");
        for (row = 0; row < nrows; row++)
        {
            for (col = 0; col < ncols; col++)
            {
                rptr[row][col] = row + col;
                printf("%d ", rptr[row][col]);
            }
            putchar('\n');
        }
        puts("\n");
    
    
        /* and here we illustrate that we are, in fact, dealing with
           a 2 dimensional array in a contiguous block of memory. */
        printf("And now we demonstrate that they are contiguous in memory\n");
        testptr = aptr;
        for (row = 0; row < nrows; row++)
        {
            for (col = 0; col < ncols; col++)
            {
                printf("%d ", *(testptr++));
            }
            putchar('\n');
        }
    
    
        return 0;
    }

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    This method uses two arrays. The first is a char array that holds the actual memory used, in this case it's called aptr. The second is an array of pointers to char that reference locations inside aptr, this one is called rptr. First the program allocates enough memory to aptr for cols * rows (in this case, 40), then allocates enough memory to rptr for the number of rows. The end result is a crude ASCII diagram:
    Code:
    -----------------------
    aptr|0|1|2|3|4|5|6|7|8|
    -----------------------
         ^     ^     ^
         |     |     |
    -----------------------
    rptr|0    |1    |2    |
    -----------------------
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Jul 2003
    Posts
    28
    do you really need to allocate memory for all the pointer though surley thats done when you declare rptr? I dont get why its using the malloc for rptr and why the program works fine when I remove it?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I dont get why its using the malloc for rptr
    Because in a realistic situation you'll most likely not know how many rows there are. Since C99 isn't in widespread use, the only way to get variable length arrays is to allocate memory to a pointer dynamically.

    >why the program works fine when I remove it?
    You're getting lucky. I crash and burn (as I should) when it's removed.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  2. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  3. Parameter passing with pointer to pointer
    By notsure in forum C++ Programming
    Replies: 15
    Last Post: 08-12-2006, 07:12 AM
  4. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM