storing bytes in a char * and printing to serial crashes esp12e

Hello y’all. So i have this program that reads bytes from flash memory on startup. Things i am storing on flash are mqtt credentials. I sucessfully wrote 170 bytes of info to flash and i painstakingly made sure every byte was correct. Now when I pull the bytes from flash, I am having a hard time making the data useful. I can get them into the String type fine, but the MQTT library I use requires a char * for the credentials.

Ive messed around all last night and today and I just cant seem to convert bytes to char . No matter what I try, the ESP board crashes when i print the char to serial. Take this sample code for example. it does exactly what my large program does to me…

char *someString = 0;
byte someByteArray[3];

void setup() 
{
  Serial.begin(115200);
  while (!Serial){};
     
  for (int i=0; i < 3; i++)
    someByteArray[i] = 0;

  someByteArray[0]=97;
  someByteArray[1]=98;
  someByteArray[2]=99;
    
  for (int i = 0; i < 5; i++)
    Serial.print(someByteArray[i]);
  Serial.println("");
    
  for (int i = 0; i < 5; i++)
    someString = someString + char(someByteArray[i]);
  
  Serial.println(someString);
}

void loop(){}

I get this for serial output. It gets through the loop of printing the someByteArray to serial but crashes before it prints the char* to serial.

979899

--------------- CUT HERE FOR EXCEPTION DECODER ---------------

Exception (28):
epc1=0x40206b56 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00000124 depc=0x00000000

>>>stack>>>

ctx: cont
sp: 3ffffdd0 end: 3fffffc0 offset: 0190
3fffff60:  40201124 3ffee2fc 3ffe8610 4020132d  
3fffff70:  40201124 3ffee2fc 00000126 40201324  
3fffff80:  3ffee2d8 3ffee2fc 3ffee2fc 40201369  
3fffff90:  3fffdad0 3ffee2fc 00000005 402010b2  
3fffffa0:  3fffdad0 00000000 3ffee324 40201a20  
3fffffb0:  feefeffe feefeffe 3ffe84e0 40100b85  
<<<stack<<<

any ideas? im sure its something simple but its evading me. i messed with strcpy, getBytes, and iota functions and other data types. im beat.

// define a pointer to char, and point it at address 0 (where important things live....)
char *someString = 0;

// define a 3 byte array
byte someByteArray[3];

void setup()
{
  Serial.begin(115200);
  while (!Serial){};
     
  // set all three bytes of someByteArray to zero
  for (int i=0; i < 3; i++)
    someByteArray[i] = 0;

  // then immediately set them to 97, 98 and 99
  someByteArray[0]=97;
  someByteArray[1]=98;
  someByteArray[2]=99;

  // now print the values of the three bytes in someByteArray, plus WHATEVER is after it in memory
  for (int i = 0; i < 5; i++)
    Serial.print(someByteArray[i]);
  Serial.println("");
   
  // now set someString to some more or less useless random values
  // someString will be set to 97, then 195, then 294, then two UNKNOWN 
  // values whatever is in memory after someByteArray
  for (int i = 0; i < 5; i++)
    someString = someString + char(someByteArray[i]);
 
  // now try to print whatever memory someString is pointing to, and nobody know what is actually there
  Serial.println(someString);
}

void loop(){}

In short, you are pretty much randomly changing the value of a pointer, but never actually pointing it to anything that remotely resembles a printable string.

Pointers must be initialized to point to valid memory address before you attempt to use them

Do not try to read or write or write 5 bytes from/to a 3-byte array

It’s really not clear what you were trying to accomplish, but this code does nothing useful, and crashes because you are trying to read memory you don’t “own”.

Try explaining what it is you’re actually trying to do.

someString = someString + char(someByteArray[i]);Well here you are treating a char* as if it's a String, but it's not a 'String'. You can not 'just add to it' .
A char* is a pointer to a series of bytes for which you have declared memory space when you've decle=ared the variable.char *someString = 0;in your case you've declared 1 byte (plus a null terminator that gets conveniently added automatically) If you write beyond that size, you will be writing to addresses that belong to other variable & pointers. If you declare a space of sufficient size

char *someString = "          ";[  // or
char someString[10] = 0;/code]You can write to locations that you have reserved (still not by simply adding to the c-string)
If you just want to add to a 'String' and then convert the result to a c-string afterwards, that is possible but not recommended.

Deva_Rishi:
char *someString = 0;in your case you've declared 1 byte (plus a null terminator that gets conveniently added automatically)

No.

OP just declared a pointer to address zero, which in most cases mean "nothing".

Whandall:
No.

OP just declared a pointer to address zero, which in most cases mean "nothing".

Oh yes i'm sorry i got confused withchar *someString = "\0";Anyway a pointer to address zero is definitely not something you want to write anything to.