For a specific project, I need to convert an int to a const int variable type.

int i = 10;
boolean ShiftRegister[i] //Throws error

const int i = 10;
boolean ShiftRegister[i]// Works Fine

as i am making my own custom library where the size of boolean can be defined by creating an object by the user

myLib Library(20); // user defined boolean size

and the size is stored to a struct like this as it can't directly be converted to a const int.

The header file ...

#ifndef myLib_h
#define myLib_h
#include "Arduino.h"

class myLib{

myLib(int input);
int input;


the C++ code

#include "myLib.h"
#include "Arduino.h"

 struct Data {
  int BoolSize;

 myLib::myLib(int input){
 const int boolSize = Data[0].BoolSize;

 bool DataBoolean[boolSize];

Whenever i run this library, it throws an error untill i dont predefine the boolean size in cpp code like this..

bool DataBoolean[20];

that means there is something error converting the int to const int in this line

const int boolSize = Data[0].BoolSize;

so what i want is, i dont want to predefine the boolean size in the cpp code. i would rather like to define boolean size by creating an object(where the code crashed), storing the size to a structure then assigning the structure integer to the const int variable. I need an efficiant way to

turn this struct variable


into a const variable and assign to it like this without any errors

const int boolSize = Data[0].BoolSize;

so how can i convert an integer to a const int so that i can continue to my project?

any helps please? thanks for your kind support.

  • 2
    struct Data { int BoolSize; } Data[0]; declares an array of zero elements. You need to change that 0 to (at least) a 1.
    – Mat
    Commented Mar 19, 2021 at 9:04
  • For this you should look at template classes.
    – Majenko
    Commented Mar 19, 2021 at 9:14
  • @Mat an array starts from number 0, and I don't want more memory for data so I have given array length 0 only . Can I not be able to access data at array position 0 then ? Commented Mar 19, 2021 at 9:43
  • @Majenko please spend a little more time for me and write an example code here that will be easier for me to understand Commented Mar 19, 2021 at 9:44
  • I will, but I'm cooking breakfast at the moment.
    – Majenko
    Commented Mar 19, 2021 at 9:45

2 Answers 2


At the time the constructor of your class is called, its size must be already defined, because the memory allocation takes place before. But you try to give it as a parameter to the constructor. This cannot work.

You have at least these alternatives. (Note: This is an invitation to edit this answer and add more, if you know some serious solution.)

1. Use a template for the size

Please be aware that each different size will generate another implementation in machine code.

template<size_t size>
class myLib {
    inline myLib<size>() {}
    bool data[size];

2. Allocate the requested memory dynamically

class myLib {
    inline explicit myLib(size_t size) : data(new bool[size]) {}
    inline ~myLib() {
        delete[] data;
    bool* data;

Note: Depending on the version of C++ you're using, you need to consider the Rule of Three (before C++11) or the Rule of Five (beginning with C++11). Since the implementation of the missing methods depends on specific details, it is left as an exercise to the reader.

  • There is a memory leak in the second example: you should delete[] data in a destructor. But see also the rule of three. Commented Mar 19, 2021 at 17:03
  • @EdgarBonet Absolutely correct, thanks! My current project is an embedded system that never destructs objects… misleading me. Therefore we don't need any of the additional methods. Commented Mar 19, 2021 at 20:04

If you want variable length of an array, then you can use malloc. With same conext, if you want to initiate an array with specific length of value you have to use malloc.

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.