public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* RE: How to pass 2D variable-sized arrays in C++?
@ 2002-09-30 14:48 Jason Mancini
  2002-09-30 18:46 ` Claudio Bley
  0 siblings, 1 reply; 9+ messages in thread
From: Jason Mancini @ 2002-09-30 14:48 UTC (permalink / raw)
  To: gcc-help


Hi again,
After many tries, I propose that this is the "correct solution".
The compiler should hide the address calculation for me -- this
is why we have compilers after all!
Thanks,
Jason Mancini


void func(int c, int r, float *fa)
{
  float (*f)[c] = (float (*)[c])fa;
  ... f[xr][xc] ...
}

main()
{
  int c = 5, r = 4;
  float f[r][c];
  func(c, r, &f[0][0]);
}


-------------------------------

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]);



_________________________________________________________________
Send and receive Hotmail on your mobile device: http://mobile.msn.com

^ permalink raw reply	[flat|nested] 9+ messages in thread
* RE: How to pass 2D variable-sized arrays in C++?
@ 2002-09-26 14:29 Jason Mancini
  0 siblings, 0 replies; 9+ messages in thread
From: Jason Mancini @ 2002-09-26 14:29 UTC (permalink / raw)
  To: gcc-help


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

^ permalink raw reply	[flat|nested] 9+ messages in thread
* RE: How to pass 2D variable-sized arrays in C++?
@ 2002-09-26  8:33 Quang Nguyen (Ngo)
  0 siblings, 0 replies; 9+ messages in thread
From: Quang Nguyen (Ngo) @ 2002-09-26  8:33 UTC (permalink / raw)
  To: 'Jason Mancini', gcc-help

Hi Jason,

Try this:

void func(int c, int r, float f[][])
        {
        printf("hello\n");
        }

main()
        {
        int c = 5, r = 7;
        float f[r][c];

        func(c, r, f);
        }

--
Quang



-----Original Message-----
From: Jason Mancini [mailto:jayrusman@hotmail.com] 
Sent: Wednesday, September 25, 2002 7:37 PM
To: gcc-help@gcc.gnu.org
Subject: How to pass 2D variable-sized arrays in C++?



Hello,
How does one type the 2D variable size array "f" below?
I've tried nasty casts that all get rejected, cheating
with void, etc, etc.  Very stumped!  Sure, I could back
up to a real float** and manually new[] and pass float**,
but that takes all the fun out!

Thanks,
Jason


void function(int c, int r, ??? f )
{
}

main() {
  int c(5), r(7);
  float f[r][c];
  function(c, r, f);
};


--


_________________________________________________________________
Join the world's largest e-mail service with MSN Hotmail. 
http://www.hotmail.com


________________________________________________________________________
This email has been scanned for all viruses by the MessageLabs SkyScan
service. For more information on a proactive anti-virus service working
around the clock, around the globe, visit http://www.messagelabs.com
________________________________________________________________________

________________________________________________________________________
This email has been scanned for all viruses by the MessageLabs SkyScan
service. For more information on a proactive anti-virus service working
around the clock, around the globe, visit http://www.messagelabs.com
________________________________________________________________________

^ permalink raw reply	[flat|nested] 9+ messages in thread
* How to pass 2D variable-sized arrays in C++?
@ 2002-09-25 19:36 Jason Mancini
  0 siblings, 0 replies; 9+ messages in thread
From: Jason Mancini @ 2002-09-25 19:36 UTC (permalink / raw)
  To: gcc-help


Hello,
How does one type the 2D variable size array "f" below?
I've tried nasty casts that all get rejected, cheating
with void, etc, etc.  Very stumped!  Sure, I could back
up to a real float** and manually new[] and pass float**,
but that takes all the fun out!

Thanks,
Jason


void function(int c, int r, ??? f )
{
}

main() {
  int c(5), r(7);
  float f[r][c];
  function(c, r, f);
};


--


_________________________________________________________________
Join the worldÂ’s largest e-mail service with MSN Hotmail. 
http://www.hotmail.com

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2002-10-01 13:45 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-09-30 14:48 How to pass 2D variable-sized arrays in C++? Jason Mancini
2002-09-30 18:46 ` Claudio Bley
2002-10-01  4:59   ` John Love-Jensen
2002-10-01  6:04     ` Claudio Bley
2002-10-01  6:21       ` John Love-Jensen
2002-10-01  6:45         ` Claudio Bley
  -- strict thread matches above, loose matches on Subject: below --
2002-09-26 14:29 Jason Mancini
2002-09-26  8:33 Quang Nguyen (Ngo)
2002-09-25 19:36 Jason Mancini

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).