0

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.

Barmar
  • 741,623
  • 53
  • 500
  • 612
kuwira
  • 15
  • 4
  • `int *store[SIZE]` is an array of pointers, not a 2D array. – Barmar May 08 '23 at 17:37
  • Quick [example](https://godbolt.org/z/hYqhWd1h1) in addition to the duplicate. – yano May 08 '23 at 17:48
  • Before asking how to define an array of pointers to matrixes, you should first decide how to represent a single matrix. Should it be a 2D array or an array of pointers to arrays which represent the individual rows? – Andreas Wenzel May 08 '23 at 23:18

4 Answers4

1

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.

John Bode
  • 119,563
  • 19
  • 122
  • 198
0

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;
}
0___________
  • 60,014
  • 4
  • 34
  • 74
0

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.

Makset
  • 11
  • 3
  • What is `int*** pointer_to_2d_array = NULL;` supposed to be? An array of pointers to matrixes? Or a pointer to such an array? Note that the question is asking for the former, not the latter. – Andreas Wenzel May 09 '23 at 01:43
  • Are you intending to use `pointer_to_2d_array` in combination with `malloc`, to create a dynamically allocated array, instead of creating a normal array? If that is the case, you should make that clear in your answer. – Andreas Wenzel May 09 '23 at 02:26
  • There is a difference between a 2D array (e.g. `int matrix[20][10];`) and an array of pointers in which each pointer points to the first element of an array, which represent an individual row (e.g. `int *matrix[20];`). It appears to me that you are using the term "2D array" for the latter, which is wrong. – Andreas Wenzel May 09 '23 at 02:27
  • @AndreasWenzel Yes, you're right, 2D arrays/Matrices and double pointers aren't the same, you know, since most of the time I used double pointers, I think those concepts kind of fused up, my bad, I've edited the answer already. Now, the thing is, you can use both for the same purpose, so I think the answer directly falls on which way you like to represent 2D arrays the most, but mostly on the size of the required array. – Makset May 09 '23 at 05:09
  • "Now, the thing is, you can use both for the same purpose" This is wrong. You can't copy these pointer tables in one go, read/save them to a file in one go or free() them on a single line. They are also significantly slower and more error prone than 2D arrays. There are a few uses of this pointer-to-pointer trick such as creating arrays of strings with individual lengths. But using it for actual 2D arrays is always wrong. Check out [Correctly allocating multi-dimensional arrays](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays) – Lundin May 09 '23 at 11:14
0

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];
Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39