Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

2d VLA argument, void f( int m, int n, int a[m][n] ) ?

0 views
Skip to first unread message

denis

unread,
Aug 10, 2009, 7:29:30 AM8/10/09
to
Folks,
how can I pass a 2d array with sizes, like

void f( int m, int n, int a[m][n] )
{
... a[j][k] = ...
?
I see claims that this is valid C99, but g++-4.2.1 gives
error: ‘m’ was not declared in this scope

So, how can I make this work ?
I don't care much about the function header or call syntax,
but a[j][k] in the body should work as fast as a[j*n+k] .
(Some suggest boost::multi_array -- anyone have an example of such an f
() with that ?)

cheers
-- denis

FÖLDY Lajos

unread,
Aug 11, 2009, 5:55:14 AM8/11/09
to

On Mon, 10 Aug 2009, denis wrote:

> Folks,
> how can I pass a 2d array with sizes, like
>
> void f( int m, int n, int a[m][n] )
> {
> ... a[j][k] = ...
> ?
> I see claims that this is valid C99, but g++-4.2.1 gives

> error: ?m? was not declared in this scope


>
> So, how can I make this work ?
> I don't care much about the function header or call syntax,
> but a[j][k] in the body should work as fast as a[j*n+k] .
> (Some suggest boost::multi_array -- anyone have an example of such an f
> () with that ?)
>
> cheers
> -- denis
>

/tmp> cat 1.c
#include <stdio.h>

void f( int m, int n, int a[m][n] )
{

a[2][3]=12345;
}

int main()
{
int a[5][6];
f(5,6,a);
printf("%d\n", a[2][3]);
}

/tmp> gcc-4.2.3 1.c && ./a.out
12345


So, what is the problem? :-)

regards,
lajos

FÖLDY Lajos

unread,
Aug 11, 2009, 5:58:19 AM8/11/09
to

Yes, it is valid C99. But C++ is based on C89.

regards,
lajos

0 new messages