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.
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() {}