Skip to main content
Arduino

Return to Answer

+ answer the edited question.
Source Link
Edgar Bonet
  • 45.1k
  • 4
  • 42
  • 81

Your keys variable is a 2D array of int, i.e. it is an array of arrays. In C++, when an array is assigned to a variable or passed as an argument, it implicitly decays to a pointer to its first element. Note, however, that the decay-to-pointer behavior is not recursive. In this case you end up with a pointer to an array of three int.

Your code should work if you give the proper type to the _keys class member and the keys constructor parameter:

class Key {
public:
 Key(int (*keys)[3], int trig1, int trig2, int trig3, int inpin);
 int getkey();
private:
 int (*_keys)[3]; // pointer to array of 3 int.
 int _trig1;
 int _trig2;
 int _trig3;
 int _inpin;
 int _keye;
};

Edit: You have completely changed the question, to the point that my answer may not be relevant anymore. In the previous version of the question, it looked like you wanted to use a ×ばつ3 2D array of numbers in the class. In the current version, you are using an array of arbitrary sizes represented as a flattened 1D array. These are quite different approaches.

If you want to use a 2D array, the number of columns has to be a compile-time constant. You have to declare the member variable as a pointer-to-array like this:

int (*_keys)[2]; // there are two columns, not four!

assign it like this:

_keys = keys; // do NOT subscript keys

and use it like this:

_val = _keys[_a][_b]; // it is a 2D array

This approach cannot work if you do not know the number of columns beforehand.

If you want to use a flattened (1D) array, then the parameter of the constructor has to be a simple int*, and you have to give it the address of the first element of the original 2D array:

Key key(&keyss[0][0], rows, cols, inp, a, b, c);

The drawback of using a flattened array is that, in order to access the elements, you have to perform explicit index calculations:

_val = _keys[_a*_cols + _b];

Your keys variable is a 2D array of int, i.e. it is an array of arrays. In C++, when an array is assigned to a variable or passed as an argument, it implicitly decays to a pointer to its first element. Note, however, that the decay-to-pointer behavior is not recursive. In this case you end up with a pointer to an array of three int.

Your code should work if you give the proper type to the _keys class member and the keys constructor parameter:

class Key {
public:
 Key(int (*keys)[3], int trig1, int trig2, int trig3, int inpin);
 int getkey();
private:
 int (*_keys)[3]; // pointer to array of 3 int.
 int _trig1;
 int _trig2;
 int _trig3;
 int _inpin;
 int _keye;
};

Your keys variable is a 2D array of int, i.e. it is an array of arrays. In C++, when an array is assigned to a variable or passed as an argument, it implicitly decays to a pointer to its first element. Note, however, that the decay-to-pointer behavior is not recursive. In this case you end up with a pointer to an array of three int.

Your code should work if you give the proper type to the _keys class member and the keys constructor parameter:

class Key {
public:
 Key(int (*keys)[3], int trig1, int trig2, int trig3, int inpin);
 int getkey();
private:
 int (*_keys)[3]; // pointer to array of 3 int.
 int _trig1;
 int _trig2;
 int _trig3;
 int _inpin;
 int _keye;
};

Edit: You have completely changed the question, to the point that my answer may not be relevant anymore. In the previous version of the question, it looked like you wanted to use a ×ばつ3 2D array of numbers in the class. In the current version, you are using an array of arbitrary sizes represented as a flattened 1D array. These are quite different approaches.

If you want to use a 2D array, the number of columns has to be a compile-time constant. You have to declare the member variable as a pointer-to-array like this:

int (*_keys)[2]; // there are two columns, not four!

assign it like this:

_keys = keys; // do NOT subscript keys

and use it like this:

_val = _keys[_a][_b]; // it is a 2D array

This approach cannot work if you do not know the number of columns beforehand.

If you want to use a flattened (1D) array, then the parameter of the constructor has to be a simple int*, and you have to give it the address of the first element of the original 2D array:

Key key(&keyss[0][0], rows, cols, inp, a, b, c);

The drawback of using a flattened array is that, in order to access the elements, you have to perform explicit index calculations:

_val = _keys[_a*_cols + _b];
Source Link
Edgar Bonet
  • 45.1k
  • 4
  • 42
  • 81

Your keys variable is a 2D array of int, i.e. it is an array of arrays. In C++, when an array is assigned to a variable or passed as an argument, it implicitly decays to a pointer to its first element. Note, however, that the decay-to-pointer behavior is not recursive. In this case you end up with a pointer to an array of three int.

Your code should work if you give the proper type to the _keys class member and the keys constructor parameter:

class Key {
public:
 Key(int (*keys)[3], int trig1, int trig2, int trig3, int inpin);
 int getkey();
private:
 int (*_keys)[3]; // pointer to array of 3 int.
 int _trig1;
 int _trig2;
 int _trig3;
 int _inpin;
 int _keye;
};
lang-cpp

AltStyle によって変換されたページ (->オリジナル) /