0

I want to make an array of pointers, where each one points to a 2D array of floats. The 2D arrays are defined like this

float a[][2] = {{1,0.5},{2,0.5},{3,0.5},{4,0.5}};
float b[][2] = {{1,1},{5,1},{4,0.5},{3,0.5},{2,0.5},{8,1}};
float c[][2] = {{1,0.333},{1,0.333},{1,0.334}};

Each one has a different amount of rows, but always 2 columns. I also have a function where I send one of those arrays as an argument, and need to use some values in them. Let's say it's something like this (there are other things related to other values I need to use, but it's not too relevant to the question)

void take_values(float arr[][2]){
  printf("%f ", arr[0][0]);
  printf("%f ", arr[0][1]);
}

In main(), I've been using the function on each of the arrays like this

take_values(a);
take_values(b);
take_values(c);

I want to find a way to iterate that function over each array. I was thinking of making an array of pointers referencing each 2D array, but I don't know how to define and use it.

  1. How can I define the array of pointers to each of the 2D arrays?
  2. How can I send the 2D arrays as arguments in the take_values function, using that array of pointers?
  3. How would I access the specific values of the arrays inside the take_values function? / Do I need to use pointers there as well or just keep it the way it is now?

2 Answers 2

2
float (*ptrs[])[2] = {a, b, c};

which reads as

        ptrs           -- ptrs
        ptrs[]         -- is an array of unknown size
      (*ptrs[])        -- of pointers
      (*ptrs[])[2]     -- to 2-element arrays
float (*ptrs[])[2]     -- of float

and we initialize it with pointers to a, b, and c.

Your function definition would look like this:

void take_values( float (**ptrs)[2] )
{
  ...
}

int main( void )
{
  float (*ptrs[])[2] = {a, b, c};
  ...
  take_values( ptrs );
}

You'd index into each array as

ptrs[i][j][k] 

in both main and take_values.

How we get there:

Each of the expressions a, b, and c will "decay" to type float (*)[2]1, so a pointer to each array could be declared as

float (*pt1)[2] = a;
float (*pt2)[2] = b;
...

etc. We just declare an array of pointers to store all three.

You'd index into each array as

ptrs[i][j][k]

Remember that a[i] is defined as *(a + i), so

*a == *(a + 0) == a[0]

This means that

(*ptrs[i])[k] == (*(ptrs[i] + 0))[k] == (ptrs[i][0])[k] == ptrs[i][0][k]

Thus,

(*(ptrs[i] + j))[k] == (ptrs[i][j])[k] == ptrs[i][j][k]

In main, since ptrs is an array of T, where T is "array of pointer to 2-element arrays of float", when you pass it to the function, it will "decay" to type "pointer to T", or "pointer to pointer to 2-element array of float".

In take_values, you're dealing with a pointer to a pointer, so

(**ptrs)[i] == (*(*ptrs + 0))[i] == (*ptrs[0])[i] == (*(ptrs[0] + 0))[i] == (ptrs[0][0])[i]

Note that you need a way to indicate how big the array of pointers is, and how many rows each of the arrays contains.


  1. Except when it is the operand of the sizeof or unary & operators, or is a string literal used to initialize a character array in a declaration, an expression of type "N-element array of T" will be converted ("decay") to an expression of type "pointer to T" and the value of the expression will be the address of the first element of the array.

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

Comments

1

You could do something like this:

float a[][2] = {{1,0.5},{2,0.5},{3,0.5},{4,0.5}};
float b[][2] = {{1,1},{5,1},{4,0.5},{3,0.5},{2,0.5},{8,1}};
float c[][2] = {{1,0.333},{1,0.333},{1,0.334}};

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

for (int i = 0; i < sizeof(arr) / sizeof(void *); i++) {
    take_values(arr[i]);
}

But of course now you would have to change the definition of arr to include any more 2D arrays you have (but you would have to do that regardless.)

So, for example, if you added another 2D array called d, then you would have to change the definition:

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

to this:

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

NOTE: Be careful with this:

i < sizeof(arr) / sizeof(void *);

If you ever pass your array of 2D arrays around, it will decay to a pointer and this won't work.

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.