Connecting Tech Pros Worldwide Help | Site Map

size function for multi-dimensional array?

  #1  
Old July 22nd, 2005, 06:11 AM
Nancy Keuss
Guest
 
Posts: n/a
Hi, is there a function that returns the dimensions of, say, a
two-dimensional array in c++?

Thanks,
N.
  #2  
Old July 22nd, 2005, 06:11 AM
osmium
Guest
 
Posts: n/a

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.


  #3  
Old July 22nd, 2005, 06:11 AM
Ron Natalie
Guest
 
Posts: n/a

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.

  #4  
Old July 22nd, 2005, 06:11 AM
Nick Hounsome
Guest
 
Posts: n/a

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);




  #5  
Old July 22nd, 2005, 06:11 AM
Jeff Schwab
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Stats for comp.lang.c (last 7 days) CLC stats answers 1 December 22nd, 2007 10:45 PM
boost multi array dynamic size marcomoeller@googlemail.com answers 1 September 7th, 2007 08:45 PM
vector resize from within function mj answers 2 March 5th, 2006 11:25 PM
Multi-column sort (urgent help needed) Dr. Ann Huxtable answers 5 July 22nd, 2005 11:17 PM
Passing an array of chars to a function jr answers 58 July 22nd, 2005 06:36 AM