Connecting Tech Pros Worldwide Help | Site Map

size function for multi-dimensional array?

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 05:11 AM
Nancy Keuss
Guest
 
Posts: n/a
Default size function for multi-dimensional array?

Hi, is there a function that returns the dimensions of, say, a
two-dimensional array in c++?

Thanks,
N.

  #2  
Old July 22nd, 2005, 05:11 AM
osmium
Guest
 
Posts: n/a
Default 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, 05:11 AM
Ron Natalie
Guest
 
Posts: n/a
Default 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, 05:11 AM
Nick Hounsome
Guest
 
Posts: n/a
Default 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, 05:11 AM
Jeff Schwab
Guest
 
Posts: n/a
Default 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';
}

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

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 220,840 network members.