1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

2-d Array

Discussion in 'C' started by kanaks_go4ex, Oct 6, 2008.

  1. kanaks_go4ex

    kanaks_go4ex New Member

    How to pass the two dimensional arrays to functions ?
    What are the ways to implement this type .
     
  2. oogabooga

    oogabooga New Member

    To pass a 2d (or higher) array you suppress the first dimension's size,
    which means you have to pass it in as another variable.
    Here is an example:
    Code:
    #include <stdio.h>
    
    #define DIM_X  5
    #define DIM_Y 10
    
    void init_a( int a[][DIM_Y], int size) {
        int x, y;
        for (x = 0; x < size; ++x)
            for (y = 0; y < DIM_Y; ++y)
                a[x][y] = x * y;
    }
    
    void print_a( int a[][DIM_Y], int size) {
        int x, y;
        for (x = 0; x < size; ++x) {
            for (y = 0; y < DIM_Y; ++y)
                printf( "%4d ", a[x][y]);
            printf( "\n");
        }
    }
    
    int main() {
      int a[ DIM_X][ DIM_Y];
      init_a( a, DIM_X);
      print_a( a, DIM_X);
      return 0;
    }
     
    shabbir likes this.

Share This Page