I am trying to implement this function in C, it requires handling different types of data, in C++ I only use a template, but in C I don’t know how to handle it.
template<class T> int EEPROM_put(int ee, const T& value) {
const byte* p = (const byte*)(const void*)&value;
unsigned int i;
for (i = 0; i < sizeof(value); i++) foo(ee++, *p++);
return i;
}
template<class T> int EEPROM_get(int ee, T& value) {
byte* p = (byte*)(void*)&value;
unsigned int i;
for (i = 0; i < sizeof(value); i++) *p++ = foo(ee++);
return i;
}
Try the following, but it only accepts one data type, and the EEPROM_get function is not working.
int EEPROM_put(int ee, u16 value){
const u8* p = (const u8*)(const void*)&value;
unsigned int i;
for (i = 0; i < sizeof(value); i++) {
eeprom_write_1byte(ee++, *p++); Delay_Ms(10);
}
return i;
}
int EEPROM_get(int ee, u16 value) {
u8* p = (u8*)(void*)&value;
unsigned int i;
for (i = 0; i < sizeof(value); i++) {
*p++ = eeprom_read_1byte(ee++); Delay_Ms(10);
}
return i;
}
Is there a way to have an equivalent in C?
New contributor
Mister soccer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.