0

I have a list of 2D arrays:

float a[][9] = { ... }
float b[][9] = { ... }
float c[][9] = { ... }

I want to have another array of pointers that point to each of the 2D arrays, like this:

what_should_be_here?? arr[] = { a, b, c }

How to achieve this?

3
  • I would rework the design. Otherwise I would write something and see what the compiler says it expects Commented Jan 25, 2013 at 5:25
  • @KarthikT so what's the better design? Commented Jan 25, 2013 at 5:30
  • It depends on the situation. Typically I find that most multidimentional arrays can be more cleanly translated to 1D struct arrays. Commented Jan 25, 2013 at 5:44

1 Answer 1

6

Use typedef to simplify declaration. Each of the element of arr is float (*)[9]. Say this type is SomeType. Then {a,b,c} means you need an array of three elements of type SomeType.

SomeType arr[] = {a,b,c};

Now the question is, what is SomeType? So here you go:

typedef float (*SomeType)[9]; //SomeType is a typedef of `float (*)[9]`

SomeType arr[] = {a,b,c}; //this will compile fine now!

As I said, use typedef to simplify declaration!

I would choose a better name for SomeType:

typedef float (*PointerToArrayOf9Float)[9];

PointerToArrayOf9Float arr[] = {a,b,c}; 

That is a longer name, but then it makes the code readable!

Note that without typedef, your code will look like this:

float (*arr[])[9] = {a,b,c};

which is UGLY. That is why I will repeat:

Use typedef to simplify declaration!

Hope that helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.