2-d Array

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

  1. kanaks_go4ex

    kanaks_go4ex New Member

    Joined:
    Jun 11, 2008
    Messages:
    16
    Likes Received:
    0
    Trophy Points:
    0
    How to pass the two dimensional arrays to functions ?
    What are the ways to implement this type .
     
  2. oogabooga

    oogabooga New Member

    Joined:
    Jan 9, 2008
    Messages:
    115
    Likes Received:
    11
    Trophy Points:
    0
    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

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice