12 October 2010

How to pass a two dimensional array in C

int a[rows][cols];
void f(int **a);

...

f(a);

...


Guess what? Compiler complains about this (a correctly implemented compiler, that is)! You cannot pass! Instead, you should declare the function prototype to be the following:

void f(int (*a)[cols]);

or

void f(int a[][cols]);


See 2.10 in http://www.lysator.liu.se/c/c-faq/c-2.html. This is a commonly made mistake!

No comments: