So i've got this 3 dimensional array that looks like this:
int map[100][100][100];
and I want to use it as a return type for a function, whether as a pointer or whatever like so:
int* World::CreateCell() {
int map[100][100][100];
return map;
}
However I cannot find the appropriate return type for a 3d array, it will not let me use an int* like you can for a 2D array.
Even things like this don't work:
int a[100][100][100];
int* b = a;
VS seems to think the data type is int*(100)(100) but that makes no sense and doesn't work.
For what it's worth, I've googled this and seen no solutions. Thank you
Since you want 3D C-style array, you need to have a pointer to a pointer to a pointer, i.e., int***. Also, you need to allocate the memory if you use a creation function like this. Otherwise, you return the statically allocated memory from that function.
Here is a quick example of how to do it:
#include <iostream>
static int*** create_cell() {
constexpr std::size_t n = 100;
int*** map = new int**[n];
for (std::size_t i = 0u; i < n; ++i) {
map[i] = new int*[n];
for (std::size_t j = 0u; j < n; ++j) {
map[i][j] = new int[n];
}
}
return map;
}
static void delete_cell(int***& map) {
constexpr std::size_t n = 100;
for (std::size_t i = 0u; i < n; ++i) {
for (std::size_t j = 0u; j < n; ++j) {
delete[] map[i][j];
}
delete[] map[i];
}
delete[] map;
}
int main()
{
int*** a = create_cell();
a[0][0][0] = 1;
std::cout << "a[0][0][0] = " << a[0][0][0] << std::endl;
delete_cell(a);
return 0;
}
It depends on your use case: BUT for modern c++ you can ease your life using containers from the stl such as std::vector and std::array. Check here for reference: std::array and std::vector
For example, you can define your 3D types:
#include <array>
#include <vector>
using vector_3d = std::vector<std::vector<std::vector<int>>>;
using array_3d = std::array<std::array<std::array<int, 100>, 100>, 100>;
and then use them as:
array_3d b;
b[0][0][0] = 1;
std::cout << "b[0][0][0] = " << b[0][0][0] << std::endl;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With