Topic: Dereferencing a multi-dimensional array
Author: "Mark M. Young" <youngmm@wku.edu>
Date: 1999/11/20 Raw View
I tried something like that in C++; however, I was not allowed to refer to
a previous parameter in the parameter list as you did with 'width'. Should
I have been able to do that or is that something only allowed by C99?
Mark M. Young
> This is one thing that C99 does more conveniently than C++. The
> equivalent C99 code using a variable length array (VLA) would be:
>
> class2_construct(struct class2*pobj,
> int height, int width, float arg[][width])
> {
> // Initialization code for *pobj from arg.
> }
>
> Now, VLA's might get added to some future version of C++. However, VLA's
> are not allowed in C99 structs, and they probably wouldn't be allowed in
> C++ structs for the same reason, which means that using them in the
> implementation of class2 would be complicated.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: 1999/11/20 Raw View
"Mark M. Young" wrote:
>
> I tried something like that in C++; however, I was not allowed to refer to
> a previous parameter in the parameter list as you did with 'width'. Should
> I have been able to do that or is that something only allowed by C99?
Yes, it is a brand new feature in C99, and not supported at all in C++.
> Mark M. Young
>
> > This is one thing that C99 does more conveniently than C++. The
> > equivalent C99 code using a variable length array (VLA) would be:
> >
> > class2_construct(struct class2*pobj,
> > int height, int width, float arg[][width])
> > {
> > // Initialization code for *pobj from arg.
> > }
> >
> > Now, VLA's might get added to some future version of C++. However, VLA's
> > are not allowed in C99 structs, and they probably wouldn't be allowed in
> > C++ structs for the same reason, which means that using them in the
> > implementation of class2 would be complicated.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Gene Bushuyev" <gbush@my-deja.com>
Date: 1999/11/15 Raw View
Mark M. Young <youngmm@wku.edu> wrote in message
news:01bf2d96$58fa3480$864406a1@pandorasbox.wku.edu...
[snip]
> class2::class2( float ** arg, int height, int width ) { ... }
> the following is not acceptable
> float a2[ 4 ][ 5 ];
> class2 obj2( a2, 4, 5 );
>
> If the comiler can dereference one level, why not two?
Because the type of float a2[4][5] is float (*)[5] and not float**. Change
declaration of your function to this:
class2::class2( float (*arg)[5], int height, int width );
and everything should compile.
Gene Bushuyev.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: 1999/11/16 Raw View
Gene Bushuyev wrote:
>
> Mark M. Young <youngmm@wku.edu> wrote in message
> news:01bf2d96$58fa3480$864406a1@pandorasbox.wku.edu...
> [snip]
> > class2::class2( float ** arg, int height, int width ) { ... }
> > the following is not acceptable
> > float a2[ 4 ][ 5 ];
> > class2 obj2( a2, 4, 5 );
> >
> > If the comiler can dereference one level, why not two?
>
> Because the type of float a2[4][5] is float (*)[5] and not float**. Change
> declaration of your function to this:
> class2::class2( float (*arg)[5], int height, int width );
> and everything should compile.
The constructor interface given in the original message suggests that he
wants height and width to be run-time variables; unfortunately, that's
not easily doable in C++. One alternative is keeping the original
definition of class2, and changing a2 as follows:
float array[4][5];
float *a2[] = {array[0], array[1], array[2], array[3]};
This costs extra space, however.
Another way is to use a one dimensional array as a substitute for a
two-dimensional one:
float a2[4*5];
class2::class2(float *arg, int height, int width);
Internally, class2 might use a pointer table just like a2 in the
previous example, or simply do the address arithmetic itself for each
access. Either way is moderately inconvenient.
This is one thing that C99 does more conveniently than C++. The
equivalent C99 code using a variable length array (VLA) would be:
class2_construct(struct class2*pobj,
int height, int width, float arg[][width])
{
// Initialization code for *pobj from arg.
}
Now, VLA's might get added to some future version of C++. However, VLA's
are not allowed in C99 structs, and they probably wouldn't be allowed in
C++ structs for the same reason, which means that using them in the
implementation of class2 would be complicated.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Mark M. Young" <youngmm@wku.edu>
Date: 1999/11/15 Raw View
I can create a constructor or function like
class1::class1( float * arg, int len ) { ... }
and the following is acceptable
float a1[ 3 ];
class1 obj1( a1, 3 );
However, if I make a constructor or function like
class2::class2( float ** arg, int height, int width ) { ... }
the following is not acceptable
float a2[ 4 ][ 5 ];
class2 obj2( a2, 4, 5 );
If the comiler can dereference one level, why not two?
Mark M. Young
YoungMM@Hera.WKU.edu
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]