I have a few reasons to define a type for a fixed length array such as this:
typedef float fixed_array_t[NX][NY];
I then want to pass references to fixed_array_t
instances around to other functions. I'm getting a compiler warning from both GCC and CLANG though I'm seeing correct behavior.
What is this compiler warning telling me and how should my code be modified to avoid the warning? Bonus, why do I have to #define
the array size? Compile-time constants apparently don't work. error: variably modified ‘fixed_array_t’ at file scope
Here is a small demonstration code:
#include <stdio.h>
#define NX 2 // also, why does const int NX = 2; not work?
#define NY 3
typedef float fixed_array_t[NX][NY];
void array_printer( const fixed_array_t arr )
{
int i, j;
for (i = 0; i < NX; i++ )
for( j=0; j < NY; j++ )
printf("Element [%d,%d]=%f\n", i,j, arr[i][j] );
}
int main( int argc, char ** argv )
{
fixed_array_t testArray = { {1,2,3}, {4,5,6} };
array_printer( testArray );
}
GCC warning:
warning: passing argument 1 of ‘array_printer’ from incompatible pointer type
CLANG warning (actually compiling equivalent code in OpenCL):
warning: incompatible pointer types passing 'fixed_array_t' (aka 'real [2][3]'), expected 'real const (*)[3]'
Yet program operation is fine:
Element [0,0]=1.000000
Element [0,1]=2.000000
Element [0,2]=3.000000
Element [1,0]=4.000000
Element [1,1]=5.000000
Element [1,2]=6.000000