Connecting Tech Pros Worldwide Help | Site Map

size function for multi-dimensional array?

Nancy Keuss
Guest
 
Posts: n/a
#1: Jul 22 '05
Hi, is there a function that returns the dimensions of, say, a
two-dimensional array in c++?

Thanks,
N.
osmium
Guest
 
Posts: n/a
#2: Jul 22 '05

re: size function for multi-dimensional array?


Nancy Keuss writes:
[color=blue]
> Hi, is there a function that returns the dimensions of, say, a
> two-dimensional array in c++?[/color]

Since C++ doesn't have two-dimensional arrays, the answer is no. In C++
one creates the *illusion* of two-dimensional arrays.


Ron Natalie
Guest
 
Posts: n/a
#3: Jul 22 '05

re: size function for multi-dimensional array?



"osmium" <r124c4u102@comcast.net> wrote in message news:bu151n$cb5o6$1@ID-179017.news.uni-berlin.de...[color=blue]
> Nancy Keuss writes:
>[color=green]
> > Hi, is there a function that returns the dimensions of, say, a
> > two-dimensional array in c++?[/color]
>
> Since C++ doesn't have two-dimensional arrays, the answer is no. In C++
> one creates the *illusion* of two-dimensional arrays.
>[/color]
Correct, you have an array of arrays.

Nick Hounsome
Guest
 
Posts: n/a
#4: Jul 22 '05

re: size function for multi-dimensional array?



"Nancy Keuss" <muse188@aol.com> wrote in message
news:d084b908.0401130801.43915c1c@posting.google.c om...[color=blue]
> Hi, is there a function that returns the dimensions of, say, a
> two-dimensional array in c++?
>
> Thanks,
> N.[/color]

If the compiler knows the first dimension then you might be able to use the
same trick as for 1D:

int a[M][N];

assert(sizeof(a)/sizeof(a[0]) == M);
assert(sizeof(a[0])/sizeof(a[0][0]) == N);




Jeff Schwab
Guest
 
Posts: n/a
#5: Jul 22 '05

re: size function for multi-dimensional array?


Nancy Keuss wrote:[color=blue]
> Hi, is there a function that returns the dimensions of, say, a
> two-dimensional array in c++?
>
> Thanks,
> N.[/color]

#include <cstddef>
#include <utility>

typedef std::pair< std::size_t, std::size_t > Dimension_Pair;

template< typename T, std::size_t n, std::size_t m >
Dimension_Pair get_dimensions( T (&)[ n ][ m ] )
{
return Dimension_Pair( n, m );
}

#include <iostream>

int main( )
{
char c[ 3 ][ 5 ];

Dimension_Pair dimensions = get_dimensions( c );

std::cout << dimensions.first << ", " << dimensions.second
<< '\n';
}

Closed Thread