Passing a pointer to a multidimensional array to a function. - C Array

C examples for Array:Multidimensional Arrays

Description

Passing a pointer to a multidimensional array to a function.

Demo Code

#include <stdio.h>

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

int main( void )
{
   int  multi[3][4] = { { 1, 2, 3, 4 },
                      { 5, 6, 7, 8 },/*from  w  w w.j  av a  2s.  com*/
                      { 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;

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

   printarray_2(multi, 3);
   return 0;
}

void printarray_1(int (*ptr)[4]){
     int *p, count;
     p = (int *)ptr;

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

void printarray_2(int (*ptr)[4], int n)
{
     int *p, count;
     p = (int *)ptr;

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

Result


Related Tutorials