saving strings to EEPROM

hello,

i am struggling to understand why pointers(?) are being stored to EEPROM instead of the character sequences in the following code:

#include <EEPROM.h>

struct MyData {
    String att1;
    String att2;
  };

String Vars[5] = {"FOO", "BAR", "BAZ"};

void setup() {
  Serial.begin(9600);
 
  MyData data = {
    Vars[0],
    Vars[1]
  };
  MyData data2 = {
    "FOO2",
    "BAR2",
  };
  Serial.println(data.att1);
  Serial.println(data.att2);
  EEPROM.put(0, data);
  EEPROM.put(10, data2);
  for(int i=0;i<50;i++)Serial.println(EEPROM.read(i));
}

void loop() {
  
}

help is greatly appreciated. thank you, jose

Because Strings are pointers to data on the heap.

A String is a class that wraps a string. It is, therefore, useless, as it is the string that is of interest. Quit using Strings.

Dropping String in favour of char. Now FOO2 and BAR2 works but FOO and BAR only gives me “F” and “B”. What am I doing wrong?

#include <EEPROM.h>

struct MyData {
    char att1[5];
    char att2[5];
  };

char* Vars[5] = {"FOO", "BAR", "BAZ"};

void setup() {
  Serial.begin(9600);
 
  MyData data = {
    *(Vars[0]),
    *(Vars[1])
  };
  MyData data2 = {
    "FOO2",
    "BAR2",
  };
  Serial.println(data.att1);
  Serial.println(data.att2);
  EEPROM.put(0, data);
  EEPROM.put(10, data2);
  for(int i=0;i<50;i++)Serial.println(EEPROM.read(i));
}

void loop() {
 
}
  MyData data2 = {
    "FOO2",
    "BAR2",
  };

This copies FOO2 and BAR2 into the att1 and att2 members.

  MyData data = {
    *(Vars[0]),
    *(Vars[1])
  };

This does NOT copy the entire array from Vars[0] or Vars[1] to att1 or att2.

if Vars is an array of pointers, where each of its members points to an array of characters, why *(Vars[0]) does not read as: whatever what the first pointer is pointing to (="FOO")?

why *(Vars[0]) does not read as: whatever what the first pointer is pointing to (="FOO")?

*Vars would be a pointer. Vars[0] is a character. You have made a pointer to a character, not a pointer to an array of characters. The pointer points to 'F'.

thank you, figured it out!

#include <EEPROM.h>

struct MyData {
    char att1[5];
    char att2[5];
  };

char* Vars[5] = {"FOO", "BAR", "BAZ"};

void setup() {
  Serial.begin(9600);
 
  MyData data = {
    Vars[0][0],
    Vars[0][1],
    Vars[0][2],
    Vars[0][3],
    0,
    Vars[1][0],
    Vars[1][1],
    Vars[1][2],
    Vars[1][3],
    0
  };
  MyData data2 = {
    "FOO2",
    "BAR2",
  };
  Serial.println(data.att1);
  Serial.println(data.att2);
  EEPROM.put(0, data);
  EEPROM.put(10, data2);
  for(int i=0;i<50;i++)Serial.println(EEPROM.read(i));
}

void loop() {
 
}

jose1711:
thank you, figured it out!

  EEPROM.put(0, data);

EEPROM.put(10, data2);
}

I get that you know that your struct is 10 bytes, but it may be easier to just use sizeof() to position the next... makes it easier to iterate over many (you can put them into an array!).

EEPROM.put(sizeof(MyData) * 0, data);
EEPROM.put(sizeof(MyData) * 1, data2);

)