size function for multi-dimensional array?

N

Nancy Keuss

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

Thanks,
N.
 
R

Ron Natalie

osmium said:
Since C++ doesn't have two-dimensional arrays, the answer is no. In C++
one creates the *illusion* of two-dimensional arrays.
Correct, you have an array of arrays.
 
O

osmium

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

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

Nick Hounsome

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

Thanks,
N.

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

Jeff Schwab

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

Thanks,
N.

#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';
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top