tags:

views:

76

answers:

3

I have a number of arrays

double foo[][2] = { {1.0,3.0}, {2.6,10.0}, {0.0,0.0} };
double bar[][2] = { {1.4,3.2}, {2.1,9.9}, {2.1,9.9}, {2.1,9.9}, {0.0,0.0} };

So these are both of type:

double (*)[2]

I want to make an array of these so I need to declare an array of type pointer to array[2]

double ((*array)[2])[] = {foo, bar};

This syntax doesn't work - is it possible to do this in C.

I also tried to create a type but this didn't work either:

typedef double *mytype[2] ;
mytype array[] = {foo, bar};
+3  A: 

The two arrays are not of type double(*)[2]. foo is of type double [3][2] and bar is of type double [5][2]. The outer array size is implied, not absent.

To create an array of these, use them as pointers, thus:

typedef double (*mytype)[2];
mytype array[] = { foo, bar };
Marcelo Cantos
The outer array size can be implied. This isn't the issue I see it is creating an array with the 1st element foo and the 2nd bar.
Callum
The two types are different, so you can't have an array that holds both. So the fact that the implied sizes are different is quite significant. Anyway, the code I presented compiles and runs.
Marcelo Cantos
It does work. It is not purely pointer arithmetic that is being used to find the value of array[0][2][0]. array[0] decays into foo : so foo[2][0].
Callum
A: 
double ((*array)[2])[] = {foo, bar};

This don't work, because the memory location of bar is not relative to the memory location of foo, and the type of foo and bar isn't equal with the type of elements of double (*)[2] which have to be double type.

PC2st
foo and bar decay into pointers - the issue is how to create an array of pointers to arrays.
Callum
A: 

The correct syntax to do it without the typedef is:

double (*array[])[2] = {foo, bar};
caf