I'm learning library making on arduino , I want to make a library that works with arrays a user sets, e.g:

User sets array of int, when a certain function is been called the library will check if some instance are true or not ann set the return function accordingly as the array.

Code example:

    //in project example

#include <Keyss.h>

int rows=4;
int cols=2;
int a=0;
int b=1;
int c=2;
int inp=3;
int keyss[4][2]={

Key key((keyss),rows,cols,inp,a,b,c);

void setup(){
int keyprint=key.getkey();
Serial.println(key print);
void loop(){

And this will be the library files Keys.h

#ifndef KEYSS_H
#define KEYSS_H
#include <Arduino.h>

class Key {


Key(int (*keys)[4],int rows,int cols,int inp,int t1,int t2,int t3);
int getkey();
int waitforkey();


int *_keys;
int _rows;
int _cols;
int _inp;
int _t1;
int _t2;
int _t3;



And Keys.CPP

#include <Keyss.h>


Key::Key(int (*keys)[4],int rows,int cols,int inp,int t1,int t2,int t3){





int Key::getkey(){

int _a;
int _b;
int _i=0;
int _keymap=false;
int _val;
int _dt=500;
int _s1;
int _input;





  if(_input==true && _keymap==false){





return _val;



int Key::waitforkey(){

int _waitfor=getkey();




return _waitfor;


Where I have problem now is I don't know how to use or assign an array in a library I don't know how to use functions to refer to an array a user inputes, like here

int keyss[4][2]={

How do I use a function or inside the library to refer to the array a user sets?

Please how do I achieve that?

Its giving this error error message

1 Answer 1


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 {
    Key(int (*keys)[3], int trig1, int trig2, int trig3, int inpin);
    int getkey();

    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×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];
  • I tried it but I don't know the correct way to do it Jan 16, 2023 at 7:13
  • Thanks bro I didn't understand it that much earlier and also I am new to programming, but now I have understand it more Jan 16, 2023 at 14:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.