If your compiler supports variable length arrays then you can allocate the array the following way
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void f( size_t n1, size_t n2, size_t n3, char s[n1][n2][n3] )
{
for ( size_t i = 0; i < n1; i++ )
{
if ( i < n1 / 2 )
{
strcpy( s[i][0], "zero" );
}
else
{
strcpy( s[i][0], "one" );
}
if ( i % 2 == 0 )
{
strcpy( s[i][1], "zero" );
}
else
{
strcpy( s[i][1], "one" );
}
}
}
int main(void)
{
enum { N1 = 4, N2 = 2, N3 = 5 };
char ( *p )[N2][N3] = malloc( sizeof( char[N1][N2][N3] ) );
f( N1, N2, N3, p );
for ( size_t i = 0; i < N1; i++ )
{
printf( "\"%s\" \"%s\"\n", p[i][0], p[i][1] );
}
free( p );
return 0;
}
The program output is
"zero" "zero"
"zero" "one"
"one" "zero"
"one" "one"
Or you can indeed allocate an array of arrays of arrays.
For example
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void f( char ***s, size_t n1, size_t n2, size_t n3 )
{
for ( size_t i = 0; i < n1; i++ )
{
if ( i < n1 / 2 )
{
strcpy( s[i][0], "zero" );
}
else
{
strcpy( s[i][0], "one" );
}
if ( i % 2 == 0 )
{
strcpy( s[i][1], "zero" );
}
else
{
strcpy( s[i][1], "one" );
}
}
}
int main(void)
{
enum { N1 = 4, N2 = 2, N3 = 5 };
char ***p = malloc( N1 * sizeof( char ** ) );
for ( size_t i = 0; i < N1; i++ )
{
p[i] = malloc( N2 * sizeof( char * ) );
for ( size_t j = 0; j < N2; j++ )
{
p[i][j] = malloc( N3 );
}
}
f( p, N1, N2, N3 );
for ( size_t i = 0; i < N1; i++ )
{
printf( "\"%s\" \"%s\"\n", p[i][0], p[i][1] );
}
for ( size_t i = 0; i < N1; i++ )
{
for ( size_t j = 0; j < N2; j++ )
{
free( p[i][j] );
}
free( p[i] );
}
free( p );
return 0;
}
Atain the program output is
"zero" "zero"
"zero" "one"
"one" "zero"
"one" "one"
There is also a third approach where the number of pointers can be less than in the last case. All you need is to allocate a one dimensional array of pointers to first elements of two dimensional arrays.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum { N1 = 4, N2 = 2, N3 = 5 };
void f( char ( **s )[N3], size_t n1 )
{
for ( size_t i = 0; i < n1; i++ )
{
if ( i < n1 / 2 )
{
strcpy( s[i][0], "zero" );
}
else
{
strcpy( s[i][0], "one" );
}
if ( i % 2 == 0 )
{
strcpy( s[i][1], "zero" );
}
else
{
strcpy( s[i][1], "one" );
}
}
}
int main(void)
{
char ( **p )[N3] = malloc( N1 * sizeof( char ( * )[N3] ) );
for ( size_t i = 0; i < N1; i++ )
{
p[i] = malloc( N2 * sizeof( char[N3] ) );
}
f( p, N1 );
for ( size_t i = 0; i < N1; i++ )
{
printf( "\"%s\" \"%s\"\n", p[i][0], p[i][1] );
}
for ( size_t i = 0; i < N1; i++ )
{
free( p[i] );
}
free( p );
return 0;
}
And at last (I hope) there is a forth approach to declare at first an array of pointers to arrays.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
enum { N1 = 4, N2 = 2, N3 = 5 };
void f( char * ( *s )[N2], size_t n1, size_t n3 )
{
for ( size_t i = 0; i < n1; i++ )
{
if ( i < n1 / 2 )
{
strcpy( s[i][0], "zero" );
}
else
{
strcpy( s[i][0], "one" );
}
if ( i % 2 == 0 )
{
strcpy( s[i][1], "zero" );
}
else
{
strcpy( s[i][1], "one" );
}
}
}
int main(void)
{
char * ( *p )[N2] = malloc( N1 * sizeof( char * [N2] ) );
for ( size_t i = 0; i < N1; i++ )
{
for ( size_t j = 0; j < N2; j++ )
{
p[i][j] = malloc( N3 * sizeof( char ) );
}
}
f( p, N1, N3 );
for ( size_t i = 0; i < N1; i++ )
{
printf( "\"%s\" \"%s\"\n", p[i][0], p[i][1] );
}
for ( size_t i = 0; i < N1; i++ )
{
for ( size_t j = 0; j < N2; j++ ) free( p[i][j] );
}
free( p );
return 0;
}
If the compiler indeed supports variable length array then the first approach is the best.
Note: In the approaches sometimes some parameter as for example n2
is not used because I knwo that it is equal to 2
. But in general it should be specified.