public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Claudio Bley" <bley@cs.uni-magdeburg.de>
To: gcc-help@gcc.gnu.org
Subject: OT: RE: How to pass 2D variable-sized arrays in C++?
Date: Thu, 26 Sep 2002 12:14:00 -0000	[thread overview]
Message-ID: <15763.23621.22922.212189@wh2-19.st.uni-magdeburg.de> (raw)
In-Reply-To: <E50A109EE98AA049BAA09D725DB0714F38A874@yt-internet.tapeware.com>

>>>>> "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 / \

      reply	other threads:[~2002-09-26 19:14 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-09-26  8:33 Quang Nguyen (Ngo)
2002-09-26 12:14 ` Claudio Bley [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=15763.23621.22922.212189@wh2-19.st.uni-magdeburg.de \
    --to=bley@cs.uni-magdeburg.de \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).