Title: Is it possible to write a function which can take array of n dimensions?
Tags: c++;c
Question: I am trying to write a function which can take array of any dimension and print values in the array successfully. But I am not able to move forward because we have to declare all the dimensions except left most one when declaring function. Is there any possibility that we can write a generalised function which can take array as input for any dimensions?
for example, the function should be able to take 2 dimensional array or 3 dimensional array or n dimensional array where n is any number.
Comment: The answer would also seem to depend on whether this is a C or a C++ question, since I don't think templates exist in C.

Comment: It might be possible with [variadic templates](http://en.cppreference.com/w/cpp/language/parameter_pack).

Comment: You may be able to use a template in some way, but that would open the program to error, especially because it sounds like you are working with a team.

Comment: @JoachimPileborg It depends if the generation is random, or predefined; a variadic template would work for pre-defined not random.

Here is the accepted answer: Using recursion for each dimension and template (so in C++), following may help:
template <typename T&gt;
void print(const T&amp;e)
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cout << e << " ";
template <typename T, st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0size_t N&gt;
void print(const T (&amp;a)[N])
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cout << "{";
for (const auto&amp; e : a) {
print(e);
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cout << "}" << st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0endl;
Example usage:
int a[2][3][4];
print(a);
Live example
Comment for this answer: Excellent. Till now I was thinking that we can't do even in C++ also.
Comment for this answer: @RaydelMiranda: The *for range* can be written to be C++03 compatible if needed (a simple `print(a[i]);` does the job).
Comment for this answer: @RaydelMiranda: `for (st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0size_t i = 0; i != N; ++i) { print(a[i]); }`
Comment for this answer: This applies to compilers supporting C++11.
Comment for this answer: Could you tell me how Please? I didn't know about that.
Comment for this answer: I knowed that you can write it like this, but again, the code you post will only compile if the compiler support c++11. Is not a critic  it just a warning to readers.
Comment for this answer: I will delete my answer an upvote your's I see I get close but not enough ;p.

Here is another answer: An array is passed to a function as a pointer to the type of the array elements, regardless of the dimensionality of the array. You could have further arguments to specify the number of dimensions, n, and an array (another one) of length n specifying the number of elements in each dimension. Note that the [] notation is simply a tidy way of performing pointer addition.

Here is another answer: If you code you array as one-dimensional and then calculate the single index yourself, you can certainly have a program act as though the array was made for a variable number of dimensions.
My initial though on how to do that would be to start with a vector containing the extent of each dimension you intend to use.
The number of elements in that vector would be the number of dimensions you have.

Here is another answer: If you want to access particular element or operate on the array but if you want to create the matrix dynamically, you can use use pointers to access each element by passing the dimensions in the print function.
Since if you have a multidimensional array defined as int [][], then
x = y[a][b] is equivalent to x = *((int *)y + a * NUMBER_OF_COLUMNS + b);
Check this post for more details: How to use pointer expressions to access elements of a two-dimensional array in C?
So, if you want to print whole matrix or access any particular element, you can do like:
#include <iostream&gt;
using namespace std;
//the function print_2D_matrix receives 4 arguments: pointer to first element
//                                                   dimension of array arr, i.e. n x m
//                                                   index of the element to be printed, i.e. a and b
void print_2D_matrix(int *arr, int n, int m, int a, int b){
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++)
printf("%d ", *(arr + (i * m) + j));
printf("\n");
//go to the address just before a row, i.e. (a - 1) * NO_OF_COLUMNS
//then go to the address on b column, i.e. (a - 1) * NO_OF_COLUMNS + b
//since we started from the base address, i.e. first element( arr[0][0] ), subtract 1
printf("arr[3][3] = %d\n", *(arr + ((a - 1) * m) + b - 1));    //print arr[a][b]
int main() {
int n, m;
cin&gt;&gt;n&gt;&gt;m;
int arr[n][m];
for(int i = 0; i < n; i++)    //initialize the matrix
for(int j = 0; j < m; j++)
arr[i][j] = i * j;
print_2D_matrix((int *) arr, n, m, 3, 3);
return 0;
Output for above program (for n x m = 4 x 5) is:
0 0 0 0 0
0 1 2 3 4
0 2 4 6 8
0 3 6 9 12
arr[3][3] = 4

Here is another answer: I'm sure this violates at least one rule of the C standard but it should work in practice.  Note that it uses 0 as a sentinel value for the terminating element of any level of the array.
void print(void* p, int dim)
if (dim == 1)
int* a = (int*) p;
while (*a)
{
printf("%d ", *a++);
}
printf("\n");
else
void** a = (void**)p;
while (*a)
{
print(*a++, dim - 1);
}
void test()
int x0 [] = { 11, 12, 13, 14, 15, 0 };
int x1 [] = { 21, 22, 23, 0 };
int x2 [] = { 0 };
int x3 [] = { 41, 42, 0 };
int x4 [] = { 51, 52, 53, 0 };
int* y0 [] = { x0, x3, 0 };
int* y1 [] = { 0 };
int* y2 [] = { x1, x2, x4, 0 };
int** z [] = { y0, y1, y2, 0 };
print(z, 3);
Prints:
11 12 13 14 15
21 22 23
51 52 53
Comment for this answer: Please comment when downvoting.
Comment for this answer: You may add some `const`.

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 5, 'CommentCount': 4, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2014:00:09/19/14', 'FavoriteCount': 2.0, 'PostTypeId': 1, 'Tags': '<c++><c>', 'Title': 'Is it possible to write a function which can take array of n dimensions?', 'ViewCount': 339, 'id': 25941249, 'user_AboutMe': '', 'user_AccountId': 3776759.0, 'user_DisplayName': 'kadina', 'user_Reputation': 4706.0, 'user_Views': 430.0, 'user_id': 3137388.0}}