user29820: 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 < NUMBER_OF_BANKS; i++) {
  Bank bank;
  _banks[i] = &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 = &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]->getPreset('A')->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 < NUMBER_OF_BANKS; i++) {
    Bank bank;
    _banks[i] = &bank;
  }
  _currentBankIndex = 0;
  Serial.println(_banks[0]->getPreset('A')->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 = &preset; break;
    case PRESET_LOCATION_2:
      _B = &preset; break;
    case PRESET_LOCATION_3:
      _C = &preset; break;
    case PRESET_LOCATION_4:
      _D = &preset; break;
  }
}

Then, above in the StoredDataManages constructor, this line is hit:
Serial.println(_banks[0]->getPreset('A')->getPresetName());

And that's where I try to log out the preset name.

user10794: void Bank::addPreset(const char* presetName, String presetData, char presetLocaton)
{
  Preset preset(presetName, presetData);

  switch (presetLocaton)
  {
    case PRESET_LOCATION_1:
      _A = &preset; break;
    case PRESET_LOCATION_2:
      _B = &preset; break;
    case PRESET_LOCATION_3:
      _C = &preset; break;
    case PRESET_LOCATION_4:
      _D = &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.

29820: <p>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.</p>

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

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

<p><code>_banks</code> is declared like this: <code>Bank* _banks[NUMBER_OF_BANKS];</code> in the <code>.h</code>file of <code>StoredDataManager</code>.</p>

<p>A bank has 4 presets, like this:</p>

<pre><code>Preset* _A;
Preset* _B;
Preset* _C;
Preset* _D;
</code></pre>

<p>Which I initialize as this:</p>

<pre><code>Preset preset(presetName, presetData);
...
_A = &amp;preset; ...
</code></pre>

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

<p>In the preset, the name is saved like this: <code>char _presetName[4];</code>.
It is returned like this:</p>

<pre><code>char* Preset::getPresetName() {
  return _presetName;
}
</code></pre>

<p>I try to print a preset's name like this:</p>

<pre><code>Serial.println(_banks[0]-&gt;getPreset('A')-&gt;getPresetName());
</code></pre>

<p>But, right now it simply returns <code>C§</code>.</p>

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

<p>EDIT:
Here is the code of <code>getPreset</code>:</p>

<pre><code>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;
  }
}
</code></pre>

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

<p>EDIT 2:
I start in the main arduino <code>.ino</code> file with this:</p>

<pre><code>void setup() {
  Serial.begin(9600);
  StoredDataManager storedDataManager;
}
</code></pre>

<p>This triggers this constructor:</p>

<pre><code>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());
}
</code></pre>

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

<pre><code>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);
}
</code></pre>

<p>Which calls this function:</p>

<pre><code>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;
  }
}
</code></pre>

<p>Then, above in the <code>StoredDataManage</code>s constructor, this line is hit:</p>

<pre><code>Serial.println(_banks[0]-&gt;getPreset('A')-&gt;getPresetName());
</code></pre>

<p>And that's where I try to log out the preset name.</p>
 10794: <pre><code>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;
  }
}
</code></pre>

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

<hr>

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