Passing bidimentional mtrx to functions as pointer argument

Write here if you have problems with your C source code

Moderator: Board moderators

Post Reply
playerX
Learning poster
Posts: 63
Joined: Fri Apr 25, 2003 11:57 pm
Location: Coimbra, Portugal

Passing bidimentional mtrx to functions as pointer argument

Post 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
be cool...
Larry
Guru
Posts: 647
Joined: Wed Jun 26, 2002 10:12 pm
Location: Hong Kong and New York City
Contact:

Post by Larry »

Wrap it in a struct and pass that instead..
playerX
Learning poster
Posts: 63
Joined: Fri Apr 25, 2003 11:57 pm
Location: Coimbra, Portugal

Post by playerX »

yap.. that worked.. thank you a lot!
be cool...
jakabjr
Learning poster
Posts: 56
Joined: Wed Mar 23, 2005 9:21 pm
Location: Timisoara, Romania

Post 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!
Understanding a problem in a natural way will lead to a natural solution
Post Reply

Return to “C”