Thread: Graphic explanation Bidimensinal arrays pointers and addresses

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    18

    Graphic explanation Bidimensinal arrays pointers and addresses

    Hello

    I am building on a code snippet I posted before. I m trying to see how the memory addresses are for pointers themselves and for those where they point to.

    I am using a bidimensional array, so, pointer to pointer stuff. Therefore, if I get it right, the addresses the row pointers Point To should match the addresses the column pointers are Located At.

    A bidimensional array is an array that has arrays as elements, in other words, pointers which point to the address memory where the array (of columns) start &myarray[0], which is in turn comprised of pointers.

    Here below (x) is a pointer to an array (a row) which contains pointers to another addresses, (the columns here)

    each row has a different (x) that points to a different address, but the pointers of each row have the same values because all point to the same columns?

    The ^ are different memory addresses corresponding to columns.
    Within a row they are different, but they repeat themselves for every row ?



    ^ ^ ^
    x __| ___|__| ROW 1;

    ^ ^ ^
    x __| ___|__| ROW 2

    etc
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    
    int main()  {
        
    int **mat; // Pointer to pointer
    int rows, cols, i, j, k, x, y;
    
    printf("Number of Rows: ");
    scanf("%d", &rows);
    
          mat = (int **)malloc(rows*sizeof(int*));
    // array of number of rows, each row containing pointers to columns, that is why it is a pointer to pointer, however and of course
    // each row has the pointers pointing to the same memory addresses
    // as they all point to same columns as other rows
    
    
          for(x = 0; x < rows; x ++)
          {
           printf("The pointer %d points to %p and itself is located at %p\n\n: ", x, mat[x], &mat[x]);
           
          }
    
    printf("Number of Cols: ");
    scanf("%d", &cols);
    fflush(stdin);
    
          for (i=0; i<rows; i++)  // for each row ...
          {     
            mat[i] = (int *)malloc (cols * sizeof(int)); // add these many cols
          }
    
    
    
          for (j = 0; j<rows; j ++)
          {
         
               for (k = 0; k<cols; k++)
               {
         
                   mat[j][k] = j * k;
         
             printf("Array[%d][%d]  %p and stores a value of %d\n: ", j, k,  &mat[j][k], *(*(mat+j)+k));
              }
        
         }
    
         getchar(); 
        
    }
    ============================= UPDATE ================================================== =================

    I have found another snippet which clarifies this better. I have modified it slightly to show it clearer
    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));
        
        printf("la direccion de este array en (0,0) es %p\n\n: ", &aptr);
        
        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 *));
        
        printf("la direccion de este bloque de los pointers para las rows es) %p\n\n: ", &rptr);
       
       
       
        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);
        }
    
        /* Now we illustrate how the row pointers are incremented */
        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 %p ", *(testptr++), &testptr[row]);
                
            }
            putchar('\n');
        }
        
        getchar();
    
        return 0;
    }
    Last edited by alvarito; 10-01-2011 at 02:43 PM. Reason: new code snippet

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 03-13-2011, 02:48 AM
  2. char pointers and their addresses.
    By ens_leader in forum C Programming
    Replies: 5
    Last Post: 12-31-2007, 01:48 AM
  3. Addresses and pointers help...
    By GCNDoug in forum C Programming
    Replies: 13
    Last Post: 04-07-2007, 05:37 PM
  4. Pointers, addresses, and new
    By WarBaboon in forum C++ Programming
    Replies: 7
    Last Post: 06-06-2003, 10:12 PM
  5. Pointers and their Addresses
    By incognito in forum C++ Programming
    Replies: 6
    Last Post: 12-29-2001, 07:16 PM

Tags for this Thread