Even saving char* there's no problem by doing a cast.
Yes there is; you just don't see it. I won't draw out the box and arrow pointer picture, but what you are saving to the EEPROM when you do EE_WriteVariable(<PICK AN ADDRESS>, (uint16_t*)redfinal); is that you're storing the address of the string variable, not the contents of that string. So, if you read back that same address
with the same program, then the pointer points to the same string. If you wrote the address of the string to the EEPROM, then reprogrammed the chip with a new program, the chance that the value you read back (what redfinal points to) would be the same is very low.
the variable redfinal is stored and recover in the variable a
No it's not (for the same reason as above).
What you need to do is to write the bytes of the string, not the address of the string, to the EEPROM.
I don't do STM, so the below is untested (I don't even know that it will compile):
Try:
// These two are the same type variable, by the way, so I prefer to write them both the second way.
char str[] = "Hello World";
char *redfinal="HOLA";
char *result = (char *)malloc(strlen(str)+1);
// This is a more idiomatic way of copying a string in C/C++ (though your way worked)
char *p = results, char *q = str;
while (*p++ = *q++);
// Here's how I'd actually do it:
// Use the FLASH_ProgramHalfWord() or FLASH_ProgramWord() functions to write the string contents to a pre-determined location
// I'd probably write the string length first, then the string contents.
// Using the functions you've chosen to use, I'd write it like this
// still write the string length to the EEPROM first, then the contents, but if the string length is dynamic, it's going to screw up the rest of your variables, unless you only have one dynamic length string and it's the last one.
uint16_t len = strlen(result);
// Write the length
EE_WriteVariable(VirtAddVarTab[5], len);
// Loop over the string and write it two bytes at a time
for (int index = 0; index <= (len+1)/ 2; index++) {
uint16_t temp = *((uint16_t *) (result+index*2)); // This doesn't strictly meet the C lang specs, but generally works
EE_WriteVariable(VirtAddVarTab[6+index], temp);
}
uint16_t len2;
EE_ReadVariable(VirtAddVarTab[5], &len2);
char *str2 = (char *) malloc(len2+1);
for (int index = 0; index < ((len2 +1)/2); index++) {
EE_ReadVariable(VirtAddVarTab[6+index], str2+index*2);
}
str2[len2] = 0;
My apologies if it doesn't compile or doesn't work straight out of the box. I had to just write it in the EEV forum editor; I couldn't really test it, but I'm happy to work with you more as you test it.
Edit: Changed the read loop to fix an off-by-one error. This code is some hacky shit; I'm sorry that I didn't have time to pretty it up, but this at least should show you how you need to do it.
Edit2: If you could come back to the thread and let us know it worked, that might help the next random dev who is searching for the answer to this problem. (Or if it didn't, let's help some more so we do get it fixed.)