Callum
Callum

Reputation: 2133

Creating array of 2d arrays

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};

Upvotes: 2

Views: 378

Answers (3)

caf
caf

Reputation: 239041

The correct syntax to do it without the typedef is:

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

Upvotes: 2

Marcelo Cantos
Marcelo Cantos

Reputation: 185852

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 };

Upvotes: 3

Sadeq
Sadeq

Reputation: 8033

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.

Upvotes: 0

Related Questions