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-26  8:33 Quang Nguyen (Ngo)
  2002-09-26 12:14 ` OT: " Claudio Bley
  0 siblings, 1 reply; 2+ 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] 2+ messages in thread

* OT: RE: How to pass 2D variable-sized arrays in C++?
  2002-09-26  8:33 How to pass 2D variable-sized arrays in C++? Quang Nguyen (Ngo)
@ 2002-09-26 12:14 ` Claudio Bley
  0 siblings, 0 replies; 2+ messages in thread
From: Claudio Bley @ 2002-09-26 12:14 UTC (permalink / raw)
  To: gcc-help

>>>>> "Quang" == Quang Nguyen (Ngo) <quang.nguyen@tapeware.com> writes:

    Quang> Hi Jason, Try this:

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

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

    Quang>         func(c, r, f); }

Note, that this is no valid C++ code. You may just leave out the first
dimension of an multidimensional array ( e.g. f[5][] or f[5][4][8][] )
because the compiler needs this information in order to find the
position of an element.

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


To the OP: Please post this sort of language specific (non GCC
specific) questions in the appropriate forum next time. And may I
suggest that you get a good book on C++ programming?

-- 
Claudio Bley                                 ASCII ribbon campaign (")
Debian GNU/Linux advocate                     - against HTML email  X 
http://www.cs.uni-magdeburg.de/~bley/                     & vCards / \

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

end of thread, other threads:[~2002-09-26 19:14 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-09-26  8:33 How to pass 2D variable-sized arrays in C++? Quang Nguyen (Ngo)
2002-09-26 12:14 ` OT: " Claudio Bley

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).