2

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 Answers 3

3

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 };
Sign up to request clarification or add additional context in comments.

3 Comments

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.
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.
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].
2

The correct syntax to do it without the typedef is:

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

Comments

0
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.

1 Comment

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.