How to store a string in an object and retrieve it safely later? I'm writing a program where I will be able to toggle between multiple banks, and each bank has 4 presets on it. Right now, I'm trying to give each preset a name that is assigned on creation.

I have an object I called StoredDataManager, which will handle all the banks. In its constructor, I do this:

for (byte i = 0; i &lt; NUMBER_OF_BANKS; i++) {
  Bank bank;
  _banks[i] = &amp;bank;
}


_banks is declared like this: Bank* _banks[NUMBER_OF_BANKS]; in the .hfile of StoredDataManager.

A bank has 4 presets, like this:

Preset* _A;
Preset* _B;
Preset* _C;
Preset* _D;


Which I initialize as this:

Preset preset(presetName, presetData);
...
_A = &amp;preset; ...


Where presetName is "CHR", and presetData doesn't matter, because I'm just trying to get the name to work.

In the preset, the name is saved like this: char _presetName[4];.
It is returned like this:

char* Preset::getPresetName() {
  return _presetName;
}


I try to print a preset's name like this:

Serial.println(_banks[0]-&gt;getPreset('A')-&gt;getPresetName());


But, right now it simply returns C§.

Why is that? I suspect it has to do with the memory being erased or overwritten.

EDIT:
Here is the code of getPreset:

Preset* Bank::getPreset(char presetLocation) {
  switch (presetLocation) {
    case PRESET_LOCATION_1:
      return _A;
    case PRESET_LOCATION_2:
      return _B;
    case PRESET_LOCATION_3:
      return _C;
    case PRESET_LOCATION_4:
      return _D;
    default:
      return NULL;
  }
}


I think now that I'll turn this into 4 separate functions, like getA, getB and so on, but this what it is the moment. PRESET_LOCATION_1 is just a #define PRESET_LOCATION_1 'A'.

EDIT 2:
I start in the main arduino .ino file with this:

void setup() {
  Serial.begin(9600);
  StoredDataManager storedDataManager;
}


This triggers this constructor:

StoredDataManager::StoredDataManager() {
  for (byte i = 0; i &lt; NUMBER_OF_BANKS; i++) {
    Bank bank;
    _banks[i] = &amp;bank;
  }
  _currentBankIndex = 0;
  Serial.println(_banks[0]-&gt;getPreset('A')-&gt;getPresetName());
}


This is _banks: Bank* _banks[NUMBER_OF_BANKS];.
The initializing of the bank object runs this constructor: (PRESET_LOCATION_1, 2, 3, and 4 just being 'A', 'B', 'C', 'D')

Bank::Bank() {
  addPreset("CHR", "Some data", PRESET_LOCATION_1);
  addPreset("CHR", "Some data", PRESET_LOCATION_2);
  addPreset("CHR", "Some data", PRESET_LOCATION_3);
  addPreset("CHR", "Some data", PRESET_LOCATION_4);
}


Which calls this function:

void Bank::addPreset(const char* presetName, String presetData, char presetLocaton) {
  Preset preset(presetName, presetData);
  switch (presetLocaton) {
    case PRESET_LOCATION_1:
      _A = &amp;preset; break;
    case PRESET_LOCATION_2:
      _B = &amp;preset; break;
    case PRESET_LOCATION_3:
      _C = &amp;preset; break;
    case PRESET_LOCATION_4:
      _D = &amp;preset; break;
  }
}


Then, above in the StoredDataManages constructor, this line is hit:

Serial.println(_banks[0]-&gt;getPreset('A')-&gt;getPresetName());


And that's where I try to log out the preset name.
void Bank::addPreset(const char* presetName, String presetData, char presetLocaton)
{
  Preset preset(presetName, presetData);

  switch (presetLocaton)
  {
    case PRESET_LOCATION_1:
      _A = &amp;preset; break;
    case PRESET_LOCATION_2:
      _B = &amp;preset; break;
    case PRESET_LOCATION_3:
      _C = &amp;preset; break;
    case PRESET_LOCATION_4:
      _D = &amp;preset; break;
  }
}


I can't see how this is going to work because you are storing somewhere outside a function a pointer to a local variable inside the function. As soon as addPreset exits the variable preset will go out of scope, and a pointer to it will be undefined.



You should probably do a new to make a new instance of Preset which it is then valid to store outside the scope of the function.