Parsing Array Of Char in Object

Hi friends, I want to asking how to parsing array of char in an object, this is my code :

#include <EEPROM.h>
#include <SPI.h>

struct Member {
    char id[12];
};

void doAdd(Member data) {
  EEPROM.put(0, data);
  Serial.println(data.id);
}

void doRead() {
  Member data;
  EEPROM.get(0, data);
}



void setup() {
    Serial.begin(9600); // Initialize serial communications with the PC
}

void loop() {
    String params = "NOKIA";
    
    char id[params.length()];
    params.toCharArray(id, params.length()); 
    
    Member data = {
        //"NOKIA" // result : NOKIA (OK) : case no 1
        //id[params.length()] // result : K (FAILED) : case no 2
        id // result : � (FAILED) : : case no 3
    };
    doAdd(data);
    doRead();

    delay(150000);
            
}

I want to parse value array of char into object, not by hardcode like case no 1.

Really appreciate your help. :slight_smile: :slight_smile: :slight_smile:

Hello and welcome :slight_smile:

I'm not sure to understand your question, but I think you want to use function strcpy.

strcpy( data.id, id );

to parse or to pass?

if you want to initialize de memory of a variable of type Member you can use the strings functions since you have a buffer reserved for this

try this: (need to set your console to 115200 bauds)

#include <EEPROM.h>
#include <SPI.h>

struct Member {
  char id[12];
};

void doAdd(Member data) {
  EEPROM.put(0, data);
}

void doRead(Member &  data) {
  EEPROM.get(0, data);
}


void setup() {
  char params[] = "NOKIA";
  Member aBrand, aDog; // two different structures

  Serial.begin(115200); // Initialize serial communications with the PC

  // transfer in the memory allocated for aVIPMember our string
  strcpy(aBrand.id, params); // assuming it's not too long
  Serial.print("Write: "); Serial.println(aBrand.id);
  doAdd(aBrand); // store in EEPROM


  doRead(aDog); // read back from EEPROM
  Serial.print("Read: "); Serial.println(aDog.id);
}

void loop() {}

Dear Guix,

Thanks a lot my friends.

PS

note that your function to read from EEPROM will lead to disappointment

void doRead() {
  Member data;
  EEPROM.get(0, data);
}

because the Member is a local variable, so gets destroyed when the function ends. You need to read about variable scope

see my code above to see passing by reference the structure in which you want the data to end up when reading.

Dear J-M-L,

Sorry, i mean passing, you were right, thanks ...

It's okay, the code just for sample to show my problem.

Once again, thanks.

Good!