how to point a 2D array?
Dec 29, 2013 at 12:58am UTC
I tried to use a pointer int** to point to a 2 dimentional array, but failed.
could anyone tell me how, cause i want to pass a pointer points to a 2D array.
Or, is that any other ways?
Dec 29, 2013 at 1:27am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
constexpr std::size_t NCOLS = 6 ; // const std::size_t NCOLS = 6 ; // C++98
void foo( int (*pointer)[NCOLS], std::size_t nrows ) {}
void bar( int pointer[][NCOLS], std::size_t nrows ) {}
using row_type = int [NCOLS] ; // typedef int row_type[NCOLS] ; // C++98
void baz( row_type* pointer, std::size_t nrows ) {}
void foobar( row_type pointer[], std::size_t nrows ) {}
int main()
{
constexpr std::size_t NROWS = 100 ; // const std::size_t NROWS = 100 ;
int a[NROWS][NCOLS] ;
foo( a, NROWS ) ;
bar( a, NROWS ) ;
baz( a, NROWS ) ;
foobar( a, NROWS ) ;
row_type b[NROWS] ; // 2d array NROWS x NCOLS
foo( b, NROWS ) ;
bar( b, NROWS ) ;
baz( b, NROWS ) ;
foobar( b, NROWS ) ;
}
Topic archived. No new replies allowed.