1

I am making an infrared macro-keyboard, I want to store Unicode for the Keyboard class in an array inside a class I made called Keybindand to allow the user to program as many outputs as they want, I want the storage to be mutable. I currently use the String class and decode within the program but it's cpu intensive and not very practical.
I want to use an array or list, but I haven't found any way to expand the size of an array inside a class. Casting a temporary array didn't work:

  int intArray[] = {0,1,2,3,4,5};
  int* temp[7];

  for(int x = 0; x<sizeof(intArray)/sizeof(intArray[0]); x++){
    temp[x] = intArray[x];
  }
  temp[6] = 6;
  intArray = temp;

IDE spits out "incompatible types in assignment of 'int* [7]' to 'int [6]'"

2
  • The error is because ìnt* temp[7] gives you an array of 7 pointers to ints, not an array of 7 ints. Remove the *. Though I don't see, what this has to do with making the array bigger. That could be done via dynamic memory allocation (just like the String class does in the background), though that is not a good idea on Arduinos, especially on the ones with low memory, like the ones based on the Atmega328p (Uno, Nano, ...) – chrisl Jun 15 '20 at 20:43
  • 1
    Please explain clearer, what you are trying to do and how. – chrisl Jun 15 '20 at 20:44
1

I want to [...] allow the user to program as many outputs as they want

This may be problematic. The microcontroller has a limited amount of RAM: it will not be able to store arbitrarily long macros. You should consider a way of telling the user “sorry, no more memory”. Or you can be optimistic and assume the user will never try to record macros long enough to exhaust the memory... and let the firmware crash if this assumption turns out to be invalid.

I currently use the String class and decode within the program but it's cpu intensive and not very practical.

CPU intensive? You mean the allocation? Reading the contents of a String object is not CPU intensive. And what do you find impractical in the String class? At it's core, it's just a dynamically sized container for character data, which seems to be exactly what you are looking for.

I haven't found any way to expand the size of an array inside a class.

A class is a data structure of fixed size. There is no way you can expand its contents. If you need something of variable size, you will have to use dynamic allocation. You will then only store a pointer to the allocated memory in your class. This is exactly what String does. The only other option is to store an array of fixed size within the class, and make that size large enough so that you never need more.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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