Page 1 of 1

Passing bidimentional mtrx to functions as pointer argument

Posted: Sat Apr 10, 2004 11:38 pm
by playerX
is it possible?

I realized I couldn't do it and decided to google for it. nothing found.. .so I ask your help.

Thank you

Posted: Sat Apr 10, 2004 11:57 pm
by Larry
Wrap it in a struct and pass that instead..

Posted: Sun Apr 11, 2004 12:27 am
by playerX
yap.. that worked.. thank you a lot!

Posted: Wed Jun 01, 2005 3:45 pm
by jakabjr
it is possible to pass anything, it's just that things get complicated fast the more u reference.
if u have

int mat[N][M]; => void doSomething(int m[][M]); //or int *m[M]
(this is an array of arrays of int)
int *mat[M]; => void doSomething(int *m[M]); // or int **m
(this is an array of pointers to int, which can b allocated dynamically to int [][])
(this is what the main args uses :wink: )

the point is that when sending an array as parameter to a function, it 'decays' into a pointer. The first size value can be omitted since u are responsable for not surpassing allocated memory. any other size values are necessary to acces values in the table.

though complicated, reading here will explain:
http://www.eskimo.com/~scs/C-faq/s6.html
btw the page where that comes from is one of my c favourites, lots of help:
http://www.eskimo.com/~scs/C-faq/top.html

hope this helps!