<p>In <code>C</code> can I <strong>pass a multidimensional array</strong> to a function <strong>as a single argument</strong> when I don't know what the dimensions of the array are going to be?</p>

<p>Besides, my multidimensional array may contain types other than strings.</p>

[{'answer_id': 4192, 'author': 'andrewrk', 'author_id': 432, 'author_profile': 'https://Stackoverflow.com/users/432', 'pm_score': 6, 'selected': True, 'text': '<p>You can do this with any data type. Simply make it a pointer-to-pointer:</p>\n\n<pre><code>typedef struct {\n  int myint;\n  char* mystring;\n} data;\n\ndata** array;\n</code></pre>\n\n<p>But don\'t forget you still have to malloc the variable, and it does get a bit complex:</p>\n\n<pre><code>//initialize\nint x,y,w,h;\nw = 10; //width of array\nh = 20; //height of array\n\n//malloc the \'y\' dimension\narray = malloc(sizeof(data*) * h);\n\n//iterate over \'y\' dimension\nfor(y=0;y&lt;h;y++){\n  //malloc the \'x\' dimension\n  array[y] = malloc(sizeof(data) * w);\n\n  //iterate over the \'x\' dimension\n  for(x=0;x&lt;w;x++){\n    //malloc the string in the data structure\n    array[y][x].mystring = malloc(50); //50 chars\n\n    //initialize\n    array[y][x].myint = 6;\n    strcpy(array[y][x].mystring, "w00t");\n  }\n}\n</code></pre>\n\n<p>The code to deallocate the structure looks similar - don\'t forget to call free() on everything you malloced! (Also, in robust applications you should <a href="http://www.google.com/search?q=check+malloc+return" rel="noreferrer">check the return of malloc()</a>.)</p>\n\n<p>Now let\'s say you want to pass this to a function. You can still use the double pointer, because you probably want to do manipulations on the data structure, not the pointer to pointers of data structures:</p>\n\n<pre><code>int whatsMyInt(data** arrayPtr, int x, int y){\n  return arrayPtr[y][x].myint;\n}\n</code></pre>\n\n<p>Call this function with:</p>\n\n<pre><code>printf("My int is %d.\\n", whatsMyInt(array, 2, 4));\n</code></pre>\n\n<p>Output:</p>\n\n<pre><code>My int is 6.\n</code></pre>\n'}, {'answer_id': 1916200, 'author': 'John Bode', 'author_id': 134554, 'author_profile': 'https://Stackoverflow.com/users/134554', 'pm_score': 5, 'selected': False, 'text': '<p>Pass an explicit pointer to the first element with the array dimensions as separate parameters.  For example, to handle arbitrarily sized 2-d arrays of int:</p>\n\n<pre><code>void func_2d(int *p, size_t M, size_t N)\n{\n  size_t i, j;\n  ...\n  p[i*N+j] = ...;\n}\n</code></pre>\n\n<p>which would be called as</p>\n\n<pre><code>...\nint arr1[10][20];\nint arr2[5][80];\n...\nfunc_2d(&amp;arr1[0][0], 10, 20);\nfunc_2d(&amp;arr2[0][0], 5, 80);\n</code></pre>\n\n<p>Same principle applies for higher-dimension arrays:</p>\n\n<pre><code>func_3d(int *p, size_t X, size_t Y, size_t Z)\n{\n  size_t i, j, k;\n  ...\n  p[i*Y*Z+j*Z+k] = ...;\n  ...\n}\n...\narr2[10][20][30];\n...\nfunc_3d(&amp;arr[0][0][0], 10, 20, 30);\n</code></pre>\n'}, {'answer_id': 8712577, 'author': 'Pedro', 'author_id': 1127866, 'author_profile': 'https://Stackoverflow.com/users/1127866', 'pm_score': -1, 'selected': False, 'text': '<pre><code>int matmax(int **p, int dim) // p- matrix , dim- dimension of the matrix \n{\n    return p[0][0];  \n}\n\nint main()\n{\n   int *u[5]; // will be a 5x5 matrix\n\n   for(int i = 0; i &lt; 5; i++)\n       u[i] = new int[5];\n\n   u[0][0] = 1; // initialize u[0][0] - not mandatory\n\n   // put data in u[][]\n\n   printf("%d", matmax(u, 0)); //call to function\n   getche(); // just to see the result\n}\n</code></pre>\n'}, {'answer_id': 24623778, 'author': 'rslemos', 'author_id': 1535706, 'author_profile': 'https://Stackoverflow.com/users/1535706', 'pm_score': 5, 'selected': False, 'text': '<p>You can declare your function as:</p>\n\n<pre><code>f(int size, int data[][size]) {...}\n</code></pre>\n\n<p>The compiler will then do all pointer arithmetic for you.</p>\n\n<p>Note that the dimensions sizes must appear <strong>before</strong> the array itself.</p>\n\n<p>GNU C allows for argument declaration forwarding (in case you really need to pass dimensions after the array):</p>\n\n<pre><code>f(int size; int data[][size], int size) {...}\n</code></pre>\n\n<p>The first dimension, although you can pass as argument too, is useless for the C compiler (even for sizeof operator, when applied over array passed as argument will always treat is as a pointer to first element).</p>\n'}, {'answer_id': 59321586, 'author': 'Andrew Henle', 'author_id': 4756299, 'author_profile': 'https://Stackoverflow.com/users/4756299', 'pm_score': 1, 'selected': False, 'text': '<blockquote>\n  <p>In C can I pass a multidimensional array to a function as a single argument when I don\'t know what the dimensions of the array are going to be?</p>\n</blockquote>\n\n<p><strong>No</strong></p>\n\n<p>If by "single argument" you mean passing just the array without passing the array dimensions, no you can\'t.  At least not for true multidimensional arrays.</p>\n\n<p>You can put the dimension[s] into a structure along with the array and claim you\'re passing a "single argument", but that\'s really just packing multiple values into a single container and calling that container "one argument".</p>\n\n<p>You can pass an array of known type and number of dimensions but unknown size by passing the dimensions themselves and the array like this:</p>\n\n<pre><code>void print2dIntArray( size_t x, size_t y, int array[ x ][ y ] )\n{\n    for ( size_t ii = 0, ii &lt; x; ii++ )\n    {\n        char *sep = "";\n        for ( size_t jj = 0; jj &lt; y; jj++ )\n        {\n            printf( "%s%d", sep, array[ ii ][ jj ] );\n            sep = ", ";\n        }\n        printf( "\\n" );\n    }\n}\n</code></pre>\n\n<p>You would call that function like this:</p>\n\n<pre><code>int a[ 4 ][ 5 ];\nint b[ 255 ][ 16 ];\n\n...\n\nprint2dIntArray( 4, 5, a );\n\n....\n\nprintt2dIntArray( 255, 16, b );\n</code></pre>\n\n<p>Similarly, a 3-dimensional array of, for example, a <code>struct pixel</code>:</p>\n\n<pre><code>void print3dPixelArray( size_t x, size_t y, size_t z, struct pixel pixelArray[ x ][ y ][ z ] )\n{\n    ...\n}\n</code></pre>\n\n<p>or a 1-dimensional <code>double</code> array:</p>\n\n<pre><code>void print1dDoubleArray( size_t x, double doubleArray[ x ] )\n{\n    ...\n}\n</code></pre>\n\n<p><strong>BUT...</strong></p>\n\n<p>However, it can be possible to pass "arrays of pointers to arrays of pointers to ... an array of type <code>X</code>" constructs that are often mislabeled as a "multidimensional array" as a single argument as long as the base type <code>X</code> has an sentinel value that can be used to indicate the end of the final, lowest-level single-dimensional array of type <code>X</code>.</p>\n\n<p>For example, the <code>char **argv</code> value passed to <code>main()</code> is a pointer to an array of pointers to <code>char</code>.  The initial array of <code>char *</code> pointers ends with a <code>NULL</code> sentinel value, while each <code>char</code> array referenced by the array of <code>char *</code> pointers ends with a <code>NUL</code> character value of <code>\'\\0\'</code>.</p>\n\n<p>For example, if you can use <code>NAN</code> as a sentinel value because actual data won\'t <strong>ever</strong> be a <code>NAN</code>, you could print a <code>double **</code> like this:</p>\n\n<pre><code>void printDoubles( double **notAnArray )\n{\n    while ( *notAnArray )\n    {\n        char *sep = "";\n        for ( size_t ii = 0;  ( *notAnArray )[ ii ] != NAN; ii++ )\n        {\n            printf( "%s%f", sep, ( *notAnArray )[ ii ] );\n            sep = ", ";\n        }\n\n        notAnArray++;\n    }\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/4051', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/381/']