I need to store multiple matrixes (their addresses) in a array but I couldn't figure it out.
I created int* store[SIZE]
kind thing but I'm not sure.
I need to store multiple matrixes (their addresses) in a array but I couldn't figure it out.
I created int* store[SIZE]
kind thing but I'm not sure.
So if you have an array
int arr[R][C];
then the result of the expression arr
would have type
int (*)[C];
since it's not the operand of the unary &
operator and the expression "decays" from type "array of T
" to "pointer to T
" (where T
is int [C]
), while the result of the expression &arr
would have type
int (*)[R][C];
So there are two ways to go here. The first way is:
int (*ptrs[])[C] = { arr, another_arr, yet_another_arr, ... };
or
int (*ptrs[N])[C];
ptrs[0] = arr;
ptrs[1] = another_arr;
ptrs[2] = yet_another_arr;
...
All of the pointed-to arrays must have C
columns, but they may have different numbers of rows (you'd have to store that information separately, though).
Alternately, you could use
int (*ptrs[])[R][C] = { &arr, &another_arr, &yet_another_arr ... };
or
int (*ptrs[N])[R][C];
ptrs[0] = &arr;
ptrs[1] = &another_arr;
ptrs[2] = &yet_another_arr;
...
In this case all of the pointed-to arrays must have R
rows and C
columns.
You need to have an array of pointers to 2D arrays
#define ARRAY_SIZE 100
#define ROWS 10
#define COLS 20
int (*array[ARRAY_SIZE])[COLS]; //this one is actually array of pointers to 1D array.
// or
int (*array1[ARRAY_SIZE])[ROWS][COLS];
usage:
int main(void)
{
int arr[ROWS][COLS];
array[0] = arr;
array1[0] = &arr;
array[0][5][3] = 5;
(*array1[0])[5][3] = 5;
}
Yes, and you can do it in various ways.
In the case you want the data to be in the heap, your will either turn your matrix into pointers to pointers or do memory arithmetic
//Pointer to pointer
int*** array_of_pointer_for_2d_array = (int***)malloc(w*sizeof(int**));
array_of_pointer_for_2d_array[0] = (int**)malloc(y*sizeof(int*));
array_of_pointer_for_2d_array[0][0] = (int*)malloc(x*sizeof(int));
array_of_pointer_for_2d_array[0][0][0] = data;
//Pointer arithmetic
int** pointer_to_2d = (int**)malloc(a*sizeof(int*));
pointer_to_2d[0] = (int*)malloc((x*y)*sizeof(int));
int requested_data = pointer_to_2d[0][(z*y)+w];
Now in the stack, is something like, this:
int(*pointer)[x][y];
data = (*pointer)[w][z]
Now, it is important that take you in mind the limitations, upsides and downsides of each method the moment you want to declare and use an array.
Before asking how to define an array of pointers to matrixes, you must first decide how to represent an invididual matrix.
In C, a matrix can be represented as a 2D array, for example like this:
#define NUM_ROWS 20
#define NUM_COLS 10
int matrix[NUM_ROWS][NUM_COLS];
A matrix can also be represented as an array of pointers in which each pointer points to the first element of an int
array. These arrays represent the individual rows of the matrix. Here is an example:
#define NUM_ROWS 20
#define NUM_COLS 10
int *matrix[NUM_ROWS];
In both cases, you can use the expression matrix[5][8]
to retrieve the element in the 9th column of the 6th row of the matrix.
Now, to answer your question on how to define an array of pointers in which each pointer points to a matrix:
Assuming that the dimensions of all matrixes are the same, then you can do the folllowing:
If an individual matrix is represented as a 2D array, then you can define an array of pointers to the individual matrixes like this:
#define NUM_ROWS 20
#define NUM_COLS 10
#define NUM_MATRIXES 30
int (*matrixes[NUM_MATRIXES])[NUM_COLS];
If an individual matrix is represented as an array of pointers to the individual rows of the matrix, then you can define an array of pointers to the individual matrixes like this:
#define NUM_ROWS 20
#define NUM_COLS 10
#define NUM_MATRIXES 30
int **matrixes[NUM_MATRIXES];