Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

How to get the dimension/bounds of an array?

array bounds dimension
0
Posted

How to get the dimension/bounds of an array?

0

Consider: #define BOUNDS(array) sizeof(array) / sizeof(array[0]) // … char buf[99]; // …size_t is from stddef.h in C, cstddef in C++ size_t bd = BOUNDS(buf); In C++ you might do this: #include #include template inline std::size_t bounds(T (&)[N]) { return N; } int main() { char a[99]; int b[9999]; char hello[] = “Hello”; char *ptr = hello; std::cout Note that ptr IS NOT an array, and so neither bounds nor BOUNDS will work correctly with it (one could argue by design). To pick up additional bounds, you might do: template inline std::size_t bounds2(T (&)[N][N2]) { return N2; } Back to Top Comeau C++ is packed with template support. Read about it! or even Try it out!

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.