You can use std::array.
If the sizes are compiled time known, I would suggest having std::array, by which you could get contiguous memory layout.
#include <iostream>
#include <array>
#include <algorithm> // std::generate
// alias for the required array
using Array3D = std::array<std::array<std::array<int, 2>, 2>, 2 >;
Array3D func()
{
Array3D arr3d;
std::array<int, 2> a{ 10, 20 }, b{ 10, 20 }, c{ 10, 20 }; // some a, b, c
// iterate through the arrays to initialize them!
auto input = 1u;
for (std::array<std::array<int, 2>, 2> & array2D : arr3d) // or auto& array2D: arr3d
for (std::array<int, 2> & arr : array2D) // or auto& arr: array2D
std::generate(arr.begin(), arr.end(), [&input] { return input++; });
// for array multimplications!
for (std::array<std::array<int, 2>, 2> & array2D : arr3d) // or auto& array2D
{
for (std::array<int, 2> & arr : array2D) // or auto& arr
{
auto arrIndex = 0u;
for (int& ele : arr)
{
ele = a[arrIndex] * b[arrIndex] * c[arrIndex];
++arrIndex;
std::cout << ele << " "; // to print the element!
}
}
}
return arr3d; // return the array like this!
}
Alternatively using the templated function (with sizes as non-template parameters), you could even pass the array3d by reference and do the calculations just you shown above!
#include <iostream>
template<std::size_t M, std::size_t N, std::size_t O>
void func(int (&arr3d)[M][N][O])
{
// some a, b, c
for (std::size_t i = 0; i < M; ++i)
{
for (std::size_t j = 0; j < N; ++j)
{
for (std::size_t k = 0; k < O; ++k)
{
arr3d[i][j][k] = a[k] * b[k] * c[k];
std::cout << arr3d[i][j][k] << " "; // to print the element
}
std::cout << "\n";
}
std::cout << "\n";
}
// do not need to return!
}