Incorrect string return

I’m looking for an error in return of strings. The “bb.name” returns only “0123400000X”. It should be “0123400000Xab340000x”

QUESTIONS:

  1. What can cause this behavior in the wrong return of string “bb.name”?
  2. Are correct the pointer parameters passed in functions “readBlockEEPROM” and “writeBlockEEPROM” ?

P.S.: I’m using ESP8266 and Arduino IDE 1.8.5.

#include <EEPROM.h>

  struct MyObject {
    float field1;
    unsigned char field2;
    char name[20];
  };

#define MEM_ALOC_SIZE   50

void readBlockEEPROM(int memAddress, char *pData){
   MyObject ret;
   
   EEPROM.begin(MEM_ALOC_SIZE);
   EEPROM.get(memAddress, ret);
   EEPROM.end();
   delay(10);
   memcpy(&pData, &ret, sizeof(MyObject));
}

void writeBlockEEPROM(int memAddress, char *pData){
   MyObject ret;
   
   EEPROM.begin(MEM_ALOC_SIZE);
   memcpy(&ret, &pData, sizeof(MyObject));
   EEPROM.put(memAddress, ret);
   EEPROM.end();
}

void  setup() {

  Serial.begin(115200);

  MyObject aa;
  aa.field1 = 3.16f;
  aa.field2 = 66;
  strcpy(aa.name, "0123400000Xab340000x");

  Serial.println();
  Serial.println("======================");
  Serial.println(aa.name);

  writeBlockEEPROM(0, (char *)&aa);

}
 
void loop() {
  MyObject bb;

  readBlockEEPROM(0, (char *)&bb);

  Serial.printf("field1 = %f\n", bb.field1);
  Serial.printf("field2 = %d\n", bb.field2);
  Serial.print("name = ");
  Serial.println(bb.name);
  Serial.println();

  delay(500);
}

You must allocate 21 bytes to store a 20 character C-string, like the following:

  strcpy(aa.name, "0123400000Xab340000x");

Don’t forget the zero terminator.

Let me explain it differently. The code below works correctly, but the code I posted at the beginning (with custom functions), is giving trouble. I don’t know what the problem on that functions

#include <EEPROM.h>

struct MyObject {
  float field1;
  unsigned char field2;
  char name[21];
};

MyObject aa;

#define MEM_ALOC_SIZE   50

void  setup() {
	Serial.begin(115200);
  
  aa.field1 = 3.16f;
  aa.field2 = 66;
  strcpy(aa.name, "01234567890123456789");

  EEPROM.begin(MEM_ALOC_SIZE);
  EEPROM.put(0, aa);
  EEPROM.end();
  
  Serial.println("");
  Serial.println("");
}
 
void loop() {
  MyObject bb = {0, 0, ""};

  EEPROM.begin(MEM_ALOC_SIZE);
  EEPROM.get(0, bb);  
  EEPROM.end();

  Serial.printf("field1 = %f\n" , bb.field1);
  Serial.printf("field2 = %d\n" , bb.field2);
  Serial.printf("name = %s\n"   , bb.name);
  Serial.println("");

	delay(500);
}

Let me explain it differently.

We are all waiting for that explanation.

The code you posted at the beginning had an obvious error, which you have now fixed.

I don't know if you had the opportunity to test my first code, but in that code the struct is not filled correctly (although I have changed the size of array, as you pointed out). I believe that function parameters are wrong, but since I'm not sure about it, I decided to post my question.
Can someone help me?

The error I pointed out has unpredictable effects, and since the code never could have worked as expected, I did not bother to test it.

So, what is the problem you are having with the repaired code?

So, what is the problem you are having with the repaired code?

None. The second code works fine! I've used it to exemplify the problem on my first code.

In my first code there are some problem with functions parameters (i think...), so I don't know how to fix it. I need to do generic functions to work properly on WRITE and READ struct data.

there are some problem with functions parameters

What problems?

The "bb.name" returns only "0123400000X".

It should be "0123400000Xab340000x"

I give up.