This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
RE: How to pass 2D variable-sized arrays in C++?
- From: "Jason Mancini" <jayrusman at hotmail dot com>
- To: gcc-help at gcc dot gnu dot org
- Date: Thu, 26 Sep 2002 14:29:00 -0700
- Subject: RE: How to pass 2D variable-sized arrays in C++?
- Bcc:
Hello,
Thank you for the thorough explanation! That is a sol'n I
thought of, but I wasn't sure it could be applied to runtime
dynamic-sized arrays.
Thanks,
Jason Mancini
-------------------------------
Claudio Bley wrote:
The second problem is that it might not do what you expect when you
want to use it in the usual notation f[i][j] which is actually
equivalent to (*(*(f+i)+j)).
The correct solution is to use it like so:
void print_mn (int m, int n, float *f)
{
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
cout << f[i*n+j] << '\t';
}
cout << endl;
}
}
float[4][5] f;
print_mn (4, 5, &f[0][0]);
_________________________________________________________________
Join the world’s largest e-mail service with MSN Hotmail.
http://www.hotmail.com