[Bf-committers] cast from flat arrays to multidimensional arrays

onk bf-committers@blender.org
Tue, 24 Jun 2003 18:33:20 +0200


Hi there,

> The simple problem is, that C doesn't provide a "pointer to [3][3]  
> array" or so... you cannot cast it either, and the warning remains  
> active. And that whilst it's perfectly OK working code. A "short  
> cont[][3]" for me still is a way to make a pointer to such an array.
> 

You can create a pointer to a x[n][m] array, but normally you don't want 
to, unless you have an array of a multidimensional array x[n][m] again 
and iterate over it.
When you pass on multidim-arrays to a function, it normally passes a 
pointer to the first element of the array (which in this case is an 
array again). The confusing thing (IMO) is the implicit conversion, so 
you can declare it like:

func(float mat[][3])  or  func(float (*mat)[3])

where in the first case it's implicitely converted to a pointer to a 
float x[3]. The second one would be the proper reference (pointer to 
first element) to a mat[3][3] element, if you need to create a reference 
   pointer on the heap.

Little suggestion for those who want to clean up the warnings related to 
'arithb.c' functions: Use typedefs - this makes the casting look much 
more readable; like:

typedef float Matrix[3][3];  // matrix type
typedef float (*pMatrix)[3]; // 'pointer' to matrix type

Oh. And one side note: I am not sure if the problem still exists, but it 
can be dangerous to mix array declarations with pointer to array 
declarations across libraries - the compiler is then not able to do the 
implicit pointer conversion and does not even warn you...(caused me a 
bad headache some time when i declared a string via char *gna = "bla"; 
instead of char gna[] = "bla"; in a linux kernel module...)

Doei,

- Strubi