Explanation for using byte and const byte for array

I have to do a presentation for a project (with a keypad) and there's a part in the code that i got from the internet that uses a const byte for defining the columns and rows and another part that uses byte for defining the pins to the keypad.

I know that const before anything means "read only" and that the value can't be changed but i'm not really sure what that does for the array part.

Read this: Read this before posting a programming question ...

I know that const before anything means "read only" and that the value can't be changed but I'm not really sure what that does for the array part.

Yes, read only, but I don't understand what you are asking, please ask again in a different way.

you are probably aware that use of hardcoded values is not good practice because that value may be used in several places in the code and if it needs to change would need to be modified in all those places and is prone to bugs if one is left unchanged.

in C, the preprocessor supports #defines which allow you to use a symbol for the value and allows any change in value to be made in one location. since it is a constant it cannot be changes and results in an error if attempted in the code. However, there is limited type checking when using a #define.

the use of "const" instead of #define provides a type for the constant which the compiler can then check. is it's intended use as a char or integer.

in your code hexaKeys, rowPins, colPins, as well as ROWS and COLS are all variables that are not changed and therefore constant. if made "const", the compiler will report an error if the is any attempt to change their value.

helping the compiler flag warnings and errors can save hours of debugging if not months if subtle.

OP's attached picture:

ROWS and COLS are required to be const because of their use in declaring a global array, whose dimensions must be known at compile time. If the array were local instead of global (the declaration is inside setup(), loop(), or another function), then const is not required, because the array is created at runtime and can have a variable size.

Depending on how hexaKeys, rowPins, and colPins are used, they possibly could also be declared as const. It would be unusual to change the pin numbers or the keypad codes while the code is running, although I'm sure someone has probably had a reason to do so at some time. In your case, the code you posted appears identical to the code used in the example sketch for the keypad library, in which case declaring the pin arrays as const generates a warning message, because the library expects the array to not be const.

1 Like

They should probably ALL be 'const' but the person who wrote the Keypad library got lazy and didn't put in all the appropriate 'const' keywords. You will get warnings if you pass a 'pointer to const' to a function that takes an argument of 'pointer':

/Users/john/Documents/Arduino/sketch_jun20a/sketch_jun20a.ino:25:81: warning: invalid conversion from 'const byte* {aka const unsigned char*}' to 'byte* {aka unsigned char*}' [-fpermissive]
 Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
                                                                                 ^


In file included from /Users/john/Documents/Arduino/sketch_jun20a/sketch_jun20a.ino:10:0:
/Users/john/Documents/Arduino/libraries/Keypad/src/Keypad.h:78:2: note:   initializing argument 2 of 'Keypad::Keypad(char*, byte*, byte*, byte, byte)'
  Keypad(char *userKeymap, byte *row, byte *col, byte numRows, byte numCols);
  ^~~~~~


/Users/john/Documents/Arduino/sketch_jun20a/sketch_jun20a.ino:25:81: warning: invalid conversion from 'const byte* {aka const unsigned char*}' to 'byte* {aka unsigned char*}' [-fpermissive]
 Keypad customKeypad = Keypad( makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);
                                                                                 ^


In file included from /Users/john/Documents/Arduino/sketch_jun20a/sketch_jun20a.ino:10:0:
/Users/john/Documents/Arduino/libraries/Keypad/src/Keypad.h:78:2: note:   initializing argument 3 of 'Keypad::Keypad(char*, byte*, byte*, byte, byte)'
  Keypad(char *userKeymap, byte *row, byte *col, byte numRows, byte numCols);
  ^~~~~~

You can fix the Keypad library by adding the 'const' keyword every place you get such a warning (about 15 places).

1 Like