0

I'm learning C++.

I only need a matrix and SPECIALLY how many rows and columns are in the matrix. I've though that I can use the following structure:

struct map {
    int rows;
    int columns;
    int matrix[rows][columns];
}

But, it doesn't compile. There is an error on line: int matrix[rows][columns];

I have also tried:

struct map {
    int rows;
    int columns;
    int matrix[map.rows][this.columns];
}

But, it doesn't compile.

The map.matrix will have map.rows and map.columns. I have declared this way because I don't know if I can declared without specifying its dimensions.

If it is correct to do: int matrix[][];.

What do I have to do to make the map.matrix have map.rows rows and map.columns columns?

VansFannel
  • 45,055
  • 107
  • 359
  • 626
  • 4
    variable length arrays are not possible in standard C++. Go with a `vector` of `vector` – P.W May 23 '19 at 10:32
  • C++ will not allow you to make an array with runtime determined size, gcc has some extensions for this but even those don't work with structure members from the same structures, just parameters to a function. What you want is variable length arrays. – xception May 23 '19 at 10:32
  • If `rows` and `columns` are variables (i.e. not compile time constants) then the array declaration is invalid in C++. That is also true if they are (non-`const`) class members, as is true in your case. Instead of trying to create a two-dimensional array, use a `std::vector >` - the sizes can then be dynamically set at run time. – Peter May 23 '19 at 10:33
  • Basically you could use the syntax int matrix[][] but than you'd have to handle the allocation of your "struct" on your own because that effectively declares just a pointer to a matrix of ints and the structure size will only include the size of the pointer (as if you had 0 elements), I'd strongly recommend against it. – xception May 23 '19 at 10:35
  • An alternative solution than the vector> thing would be to implement your own class to handle a matrix or use one already implemented (the standard library does not have such a class) if you worry about the memory allocation overhead. – xception May 23 '19 at 10:37
  • I need a matrix and the number of rows and columns for that matrix. How can I do it? – VansFannel May 23 '19 at 10:38
  • Possible duplicate of [C++ Matrix Class](https://stackoverflow.com/questions/2076624/c-matrix-class) – xception May 23 '19 at 10:41
  • I have updated my question. Sorry, but I don't understand your comments. I have tried to clarify what I'm trying to do. – VansFannel May 23 '19 at 10:41
  • I only need a matrix and the number of rows and columns for that matrix. I'm not trying to create a MATRIX class. – VansFannel May 23 '19 at 10:43
  • I think I will not use that struct and I will use a method that will return the matrix, with two output parameters to return the number of rows and columns since I don't understand what are you trying to say. – VansFannel May 23 '19 at 10:44
  • easiest way - use a vector and always access it with matrix[r * columns + c] where r is the current row and c the current column you want to access. Although it's the simplest possible way that avoids using a class is far from elegant – xception May 23 '19 at 10:44
  • @xception That matrix will be the input for a given algorithm that I can't modify. – VansFannel May 23 '19 at 10:50
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/193824/discussion-between-xception-and-vansfannel). – xception May 23 '19 at 10:50

1 Answers1

2

In order to create an array on the stack (i.e. not allocating it on the heap with new), the size of the arrays need to be known at compile time (your rows and columns are not known at compiletime).

Alternative 1: allocate on the heap (for big arrays which don't fit on the stack) or simply use vectors (vectors use heap memory).

Alternative 2: if you really need it to be on the stack, you could use a template class:

template <size_t ROWS, size_t COLUMNS>
class MyMatrix
{
    int _matrix[ROWS][COLUMNS];
};
mfnx
  • 2,894
  • 1
  • 12
  • 28