Thread: a question with pointers

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    66

    a question with pointers

    Just a simple question, but really important

    Can someone explain to me please how do I declare a pointer to an array of more than one dimension?
    is it just

    Code:
    int *p;
    int arr[10][10];
    p=&arr[0][0];
    ad if thatīs the way for any doimension, how do I move the pointer to another direction, for example: arr[2][10]

    by just manipulating the pointer

    Thanks

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    24
    than you ! uping..........

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by louis_mine
    how do I move the pointer to another direction, for example: arr[2][10]

    by just manipulating the pointer

    Thanks
    Suppose you have defined constants NUMROWS and NUMCOLS as the number of rows and number of columns, and you have this

    Code:
    int i_array[NUMROWS][NUMCOLS];
    There are NUMROWS * NUMCOLS contiguous integers in the array, and the address of element i_array[x][y] is equal to this

    Code:
    &i_array[0][0] + NUMCOLS * x + y;
    Note that you must always know the number of columns to access the individual elements.


    Try this:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
      int i_array[2][3] = {{1, 2, 3} , {4, 5, 6}}; /* 2 rows, 3 columns */
      int *i_point;
      int j, k;
    
      for (j = 0; j < 2; j++) {
        for (k = 0; k < 3; k++) {
          printf("i_array[%d][%d] = %d,   ", j, k, i_array[j][k]);
          printf("&i_array[%d][%d] = %p\n", j, k, &i_array[j][k]);
        }
      }
    
      printf("\n\n");
    
      /* 
       * for i_array[NumRows][NumCols] the address if element i_array[x][y] is
       * given by this
       *
       *     &i_array[x][y] is equal to &i_array[0][0] + (NumCols * x) + y
       *
       *
       */
    
      i_point = &i_array[0][0];
      printf("i_point = %p\n", i_point);
    
      for (j = 0; j < 2; j++) {
        for (k = 0; k < 3; k++) {
          printf("*(i_point + 3 * %d + %d) = %d, ", j, k, *(i_point + 3*j + k));
          printf(" (i_point + 3 * %d + %d) = %p\n", j, k,  (i_point + 3*j + k));
        }
      }
    
      return 0;
    }
    This arrangement of the elements is called "row-major" ordering, since the elements of a given row are contiguous in memory. Some other languages (FORTRAN for example) store array elements with "column-major" ordering.

    Regards,

    Dave
    Last edited by Dave Evans; 02-09-2005 at 09:54 AM.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I find it easier to think of it this way:
    Code:
    char foo[ YSIZE ][ XSIZE ];
    And think along the terms of a standard grid. X axis is left to right. Y axis is top to bottom. Z axis would be to the left of the Y axis, and would be the third dimension of movement.

    Consider your monitor. The X axis is from the left side to the right side. The Y axis is from top to bottom. It just helps me keep it clear to reverse the variable names from the way you use them.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Array pointers question?
    By ben2000 in forum C Programming
    Replies: 4
    Last Post: 07-26-2007, 01:31 AM
  2. A question on Pointers & Structs
    By FJ8II in forum C++ Programming
    Replies: 4
    Last Post: 05-28-2007, 10:56 PM
  3. simple pointers question
    By euphie in forum C Programming
    Replies: 4
    Last Post: 05-25-2006, 01:51 AM
  4. Very stupid question involving pointers
    By 7smurfs in forum C Programming
    Replies: 6
    Last Post: 03-21-2005, 06:15 PM
  5. Quick Question Regarding Pointers
    By charash in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2002, 11:04 AM