How to pass and return a 3-Dimensional Array in C++? Last Updated : 21 Jun, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report In C++ a 3-dimensional array can be implemented in two ways: Using array (static)Using vector (dynamic) Passing a static 3D array in a function: Using pointers while passing the array. Converting it to the equivalent pointer type. char ch[2][2][2];void display(char (*ch)[2][2]) { . . .} Program to pass a static 3D array as a parameter: C++ // C++ code to demonstrate the above method #include <bits/stdc++.h> using namespace std; // Function to show how to pass // a 3D character array to a function void display(char (*ch)[2][2]) { for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { for (int k = 0; k < 2; k++) { cout << "ch[" << i << "][" << j << "][" << k << "] = " << ch[i][j][k] << endl; } } } } // Driver code int main() { char ch[2][2][2] = { { { 'a', 'b' }, { 'c', 'd' } }, { { 'e', 'f' }, { 'g', 'h' } } }; // Function call with 3D array as parameter display(ch); return 0; } Outputch[0][0][0] = a ch[0][0][1] = b ch[0][1][0] = c ch[0][1][1] = d ch[1][0][0] = e ch[1][0][1] = f ch[1][1][0] = g ch[1][1][1] = h Time Complexity: O(n3) Auxiliary Space: O(1) Passing 3D vector (dynamic array): When a vector is passed to a function, it can either be passed by value, where a copy of the vector is stored, or by reference, where the address of the vector is passed. Pass by value: void function(vector <vector <vector < char >>> ch) { . . .} Pass by reference (Better): void function(vector< vector < vector < char>>> &ch) { . . .} Program to pass a dynamic 3D array as a parameter: C++ // C++ code to demonstrate the initialization // and passing as a parameter (passed as reference) #include <bits/stdc++.h> using namespace std; // Parameter passed as reference void display(vector<vector<vector<char> > >& ch) { for (int i = 0; i < 2; i++) { for (int j = 0; j < 2; j++) { for (int k = 0; k < 2; k++) { cout << "ch[" << i << "][" << j << "][" << k << "] = " << ch[i][j][k] << endl; } } } } int main() { vector<vector<vector<char> > > ch = { { { 'a', 'b' }, { 'c', 'd' } }, { { 'e', 'f' }, { 'g', 'h' } } }; // Function to call function by passing as parameter display(ch); return 0; } Outputch[0][0][0] = a ch[0][0][1] = b ch[0][1][0] = c ch[0][1][1] = d ch[1][0][0] = e ch[1][0][1] = f ch[1][1][0] = g ch[1][1][1] = h Time Complexity: O(n3) Auxiliary Space: O(1) Returning a 3D array: A static array cannot be returned from a function in C++. So we have to pass a 3D vector from a function to get the functionality of returning a 3D array. vector <vector< vector <char>>> fun() { vector <vector< vector <char>>> ch; . . . return ch;} Comment More infoAdvertise with us Next Article How to Pass an Array into a Lambda Function in C++? L longcodex Follow Improve Article Tags : C++ Programs DSA Arrays Interview-Questions Practice Tags : Arrays Similar Reads How to Pass a 3D Array to a Function in C++? In C++, a 3D array is a multidimensional array that has three dimensions, i.e. it can grow in three directions. In this article, we will learn how to pass a 3D array to a function in C++. Pass a 3D Array to a Function in C++ Just like normal 1-dimensional arrays, we can't pass the array to a functio 2 min read How to Pass an Array Pointer as a Function Argument in C++? In C++, a common situation arises where we have to pass an array to a function to perform some processing. This can be done by passing an array pointer as a function argument. In this article, we will learn how to pass an array pointer as a function argument in C++. Passing Array Pointer to Function 2 min read How to Pass an Array into a Lambda Function in C++? In C++, a lambda function, also known as a lambda expression, is a way of defining an anonymous function object right at the location where it is invoked or passed as an argument to a function. In this article, we will learn how to pass an array into a lambda function in C++. Passing an Array into a 2 min read How to Return a Pointer from a Function in C++? In C++, we can return a pointer from a function which is useful when we want to return large data structures that cannot be returned by value. However, it must be done carefully to avoid memory leaks, dangling pointers, and other issues related to dynamic memory management. In this article, we will 2 min read How to Print an Array in C++? In C++, an array is a fixed-size linear data structure that stores a collection of elements of the same type in contiguous memory locations. In this article, we will learn how to print an array in C++. For Example, Input: array = {10, 20, 30, 40, 50}Output: Array Elements: 10 20 30 40 50Printing Arr 2 min read How to Copy a Vector to an Array in C++? In C++, vectors are dynamic arrays that can grow and reduce in size as per requirements. Sometimes, we may need to copy the contents of a vector to the POD array. In this article, we will learn how to copy a vector to an array in C++. Example: Input: myVec = {10,20,30,40,50,60};Output: array: {10,20 2 min read Like