Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Pointers and multidimensional arrays in C

  1. Mar 29, 2010 #1

    AK2

    User Avatar

    Code (Text):

      /* Demonstrates passing a pointer to a multidimensional */
      /* array to a function. */

      #include <stdio.h>

      void printarray_1(int (*ptr)[4]);
      void printarray_2(int (*ptr)[4], int n);

      main()
     {
        int  multi[3][4] = { { 1, 2, 3, 4 },
                           { 5, 6, 7, 8 },
                           { 9, 10, 11, 12 } };

        /* ptr is a pointer to an array of 4 ints. */

        int (*ptr)[4], count;

        /* Set ptr to point to the first element of multi. */

        ptr = multi;

        /* With each loop, ptr is incremented to point to the next */
        /* element (that is, the next 4-element integer array) of multi. */

    [b]    for (count = 0; count < 3; count++)
            printarray_1(ptr++);

        puts("\n\nPress Enter...");
        getchar();
        printarray_2(multi, 3);
        printf("\n");
        return(0);
     }

     void printarray_1(int (*ptr)[4])
     {
     /* Prints the elements of a single four-element integer array. */
     /* p is a pointer to type int. You must use a type cast */
     /* to make p equal to the address in ptr. */

          int *p, count;
          p = (int *)ptr;


          for (count = 0; count < 4; count++)
              printf("\n%d", *p++);
     }

     void printarray_2(int (*ptr)[4], int n)
     {
     /* Prints the elements of an n by four-element integer array. */

          int *p, count;
          p = (int *)ptr;

          for (count = 0; count < (4 * n); count++)
              printf("\n%d", *p++);
     } [/b]

     
    The code above is from a textbook im using to learn C language.
    I dont have a good understanding of the bolded part. But since the practice in this forum is to say what I understand I will do just that.From the beginning there is a for loop. After it there is a line where there is a call to printarray_1. The argument passed is a pointer to multi[0] (ie the first element of multi[3][4] which is also a four element array). In the definition of the function void printarray_1 after the for loop the pointer *p is incremented how come it was able to print all the integer elements of multi[0], multi[1] and multi[2]. The same thing also in the definition of void printarray_2. Also in the definition of void printarray_2, the condition of the four loop why is it count<(4*3) .
     
  2. jcsd
  3. Mar 29, 2010 #2

    Mark44

    Staff: Mentor

    One of the most important concepts in using pointers is understanding what a pointer points to, and in particular how big a chunk of memory the address in a pointer represents. How a pointer is declared plays a major role in expressions involving incrementing or decrementing a pointer, and pointer arithmetic.

    In your code you have these declarations for p and ptr:
    int * p;
    and
    int (*ptr)[4];

    p is declared to be a pointer to an int. When it is initialized with an address (as in both your display functions), it points to a location in memory that can hold a 32-bit int. Just for the sake of discussion, let's suppose that p's value is 0x40001004. When p is incremented, it will be set with the next location in memory that can hold an int, or 0x40001008, which is 4 bytes higher in memory.

    ptr is declared as a pointer to a four-element array of int. This means that when ptr gets initialized, it will be set to the address of a 16 byte chunk of memory. If, for the sake of discussion, ptr is initialized with the address 0x40002000, incrementing ptr would reset the value to 0x40002010, an address that is 16 bytes higher in memory. Note that I'm giving the memory addresses in hexadecimal, which is how they are usually represented, and that 1016 = 1610.

    Inside your two functions, the local pointer variable p is assigned the value passed in the ptr parameter. Without the cast I think the compiler would generate a warning, with a pointer being assigned the value of a different kind of pointer.

    Now for your questions. printarray_1 is designed to print the four ints in a four-element array of ints. In main, printarray_1 is called four times, once for each row in the two-dimensional array. In contrast, printarray_2 is designed to print all of the elements of a two-dimensional array with n rows, and with four ints in each row. For this reason, main calls printarray_2 just once, since printarray_2 will print all n * 4 elements of the two-d array.

    Inside printarray_2, the for loop controls printing. If the two-d array that is passed has 5 rows, with 4 ints in each row, the for loop has to iterate 5 * 4 = 20 times. In this case, it is as if the loop had been written as
    Code (Text):
    for (count = 0; count < 20; count++)
    {
       .
       .
       .
    }
     
  4. Mar 31, 2010 #3

    AK2

    User Avatar

    Im using Windows XP and Vista for my programming lessons and both are 32 bit operating systems. An int is 4 bytes. I understand that multi points to multi[0] and multi[0] also points to multi[0][0]. Thanks for the explanation.
     
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook