The simplest way would be to use your b
to just store pointers to matrices like this:
In each timestep, copy your matrix a
and insert the pointer to the copy of matrix a
into b
.
Then, you can pass b[i]
over to your function func(b[i])
.
That is basically what frsim suggests as well.
In code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAX_TIME 5
void func(int array[2][3]) {
for(size_t x = 0; x < 2; ++x) {
for(size_t y = 0; y < 3; ++y) {
printf("%i ", array[x][y]);
}
printf("\n");
}
printf("\n");
}
int main (int argc, char** argv) {
int a[2][3] = {{1, 2, 3}, {4, 5, 6}};
int b[MAX_TIME][2][3] = {0};
for(size_t time = 0; time < MAX_TIME; ++time) {
/* Calculate your new version of `a` here */
a[1][0] = time;
memcpy(b[time], a, sizeof(a));
printf("%zu ", sizeof(a));
}
for(size_t i = 0; i < MAX_TIME; ++i) {
printf("time: %zu \n", i);
func(b[i]);
}
}
Arrays bear a lot of pitfalls, in particular, an array is not a pointer.
But: If you pass an array to a function, what happens is that not the array itself will be passed into the function, but a pointer to the array.
The same would happen with a two-dimensional array: When calling a function with a two-dimensional array, what is passed into the function is not the array, but a pointer, and this pointer is still just int *
and not int **
... See e.g. this question