size function for multi-dimensional array? 
July 22nd, 2005, 06:11 AM
| | | |
Hi, is there a function that returns the dimensions of, say, a
two-dimensional array in c++?
Thanks,
N. | 
July 22nd, 2005, 06:11 AM
| | | | 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. | 
July 22nd, 2005, 06:11 AM
| | | | 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. | 
July 22nd, 2005, 06:11 AM
| | | | 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); | 
July 22nd, 2005, 06:11 AM
| | | | 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';
} |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,698 network members.
|