2-d Array

kanaks_go4ex's Avatar, Join Date: Jun 2008
Go4Expert Member
How to pass the two dimensional arrays to functions ?
What are the ways to implement this type .
oogabooga's Avatar
Ambitious contributor
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;
}