EEPROM and pointers

Hi,
I am trying to write a routine, that saves different variables to EEPROM before sending my Arduino to sleep.
Here some code:

#include <EEPROM.h>
 int address = 0;
byte myByte = 14;
long myLong = 4.1;
char myString[25];

void setup(){
        saveData();
}
void loop(){}

void saveData(){
	 //saves data to EEPROM
	 pos = write(pos, &myByte, 1);
	 pos = write(pos, &myLong, 4);
	 pos = write(pos, &myString, 25);
 }
int write(int pos, long * var, int num){
	 for (int i = 0, i < num; i++){
		 EEPROM.write(pos+i, var+i);
	 }
	 return pos+num;
 }

As far as I understand pointers, &var is the address of a variable, so write(…, &var,…), should send the (4 byte?) address of var to the function.

Above sketch creates an error, when I try to pass &myByte to the write function as I declared var as long.

How can I declare a function, that it just receives the address and doesn’t care about the variable type??? Or should I just write different functions for different variable types? (e.g.: writeINT() WriteLONG…)

Thanks for your help.

As far as I understand pointers, &var is the address of a variable

It is, but even the address has a type associated with it.

should send the (4 byte?) address of var to the function.

Addresses are not 4 bytes, on an 8 bit processor. They are two bytes.

Above sketch creates an error, when I try to pass &myByte to the write function as I declared var as long.

Of course it does. The only way that that will work is to use void * (I don't care what type the pointer points to) as the type of the 2nd argument.

Wow, thanks for the quick and good reply.
:slight_smile:

Just to make sure, my function would have to look like this now, or is there a quicker and more elegant way?

int write(int pos, long * var, int num){
	 byte* ptr = (byte*) var;        //dereference var to make it usable as byte
	 for (int i = 0, i < num; i++){
		 EEPROM.write(pos+i, *ptr++);       // increase ptr 
	 }
 return pos+num;
 }

Just to make sure, my function would have to look like this now

No. You still have pointer to long as the argument type, and that, you know, is wrong.

Yes, sorry forgot to change this when copying.

I have written this and it gives me results I don’t understand:

#include <EEPROM.h>

byte myByte = 25;
int  myInt= 24;
char c[25]="Hello";

void setup(){
  Serial.begin(9600);
  print();
  saveData();
  //now change the data
  myByte = 0;
  myInt = 0;
  c[0] = 'B';
  print();
  // and read the data back from the EEPROM
  readData();
  print();
  
  Serial.println(EEPROM.read(0));
  
}
void loop(){}


void print(){
  Serial.print("MyByte =");
  Serial.print(myByte);
  Serial.print(";   MyInt =");
  Serial.print(myInt);
  Serial.print(";   c =");
  Serial.println(c);
  Serial.println("*********************************************");
}


void saveData(){
	 //saves data to EEPROM
	 int address = 0;
	 address = write(address, &myByte, 1);
	 address = write(address, &myInt, 2);
	 address = write(address, &c, 25);
 }
 
int write(int address,void *var, int num){
	byte* ptr = (byte*)var;
	 for (int i = 0; i < num; i++){
		 EEPROM.write(address+i, *ptr++);
	 }
	 return address+num;
 }

void readData(){
	 int address = 0;
	 address = write(address, &myByte, 1);
	 address = write(address, &myInt, 2);
	 address = write(address, &c, 25);
}

int read(int address, void *var, int num){
	byte* ptr = (byte*) var;
	 for (int i = 0; i < num; i++){
		 *ptr = EEPROM.read(address+i);
		 ptr++;
	 }
	 return address+num;
	
}

The output is:
MyByte =25; MyInt =24; c =Hello


MyByte =0; MyInt =0; c =Bello


MyByte =0; MyInt =0; c =Bello


0

Shouldn’t the third line be equal to the first one???
Is my read function wrong? Or does the pointer transfer myByte somehow from RAM to EEPROM?

void readData(){
	 int address = 0;
	 address = write(address, &myByte, 1);
	 address = write(address, &myInt, 2);
	 address = write(address, &c, 25);
}

That’s a strange thing to do in a function called readData.

Oh my God, what a stupid mistake!

I guess this was what is called task fixation. I was only concentrating on pointers and just didn't see the obvious.

You just saved me an other hour on the computer. Thanks

Dear bbacke

kindly share the final code.

Thank you

rajeeviitkgp11:
Dear bbacke

kindly share the final code.

Thank you

His last post was nearly 5 years ago so I suspect that you will not get a response.

If you have a problem then I suggest that you start a new thread

Here is what the final sketch should look like:

#include <EEPROM.h>

byte myByte = 25;
int  myInt = 24;
char c[25] = "Hello";

void setup()
{
  Serial.begin(9600);
  print();
  saveData();
  
  //now change the data
  myByte = 0;
  myInt = 0;
  c[0] = 'B';
  print();
  
  // and read the data back from the EEPROM
  readData();
  print();

  Serial.println(EEPROM.read(0));
}

void loop() {}

void print()
{
  Serial.print("MyByte =");
  Serial.print(myByte);
  Serial.print(";   MyInt =");
  Serial.print(myInt);
  Serial.print(";   c =");
  Serial.println(c);
  Serial.println("*********************************************");
}

void saveData()
{
  //saves data to EEPROM
  int address = 0;
  address = write(address, &myByte, sizeof myByte);
  address = write(address, &myInt, sizeof myInt);
  address = write(address, &c, sizeof c);
}

int write(int address, void *var, size_t size)
{
  byte* ptr = (byte*)var;
  for (size_t i = 0; i < size; i++)
  {
    EEPROM.write(address++, *ptr++);
  }
  return address;
}

void readData()
{
  int address = 0;
  address = read(address, &myByte, sizeof myByte);
  address = read(address, &myInt, sizeof myInt);
  address = read(address, &c, sizeof c);
}

int read(int address, void *var, size_t size)
{
  byte* ptr = (byte*) var;
  for (size_t i = 0; i < size; i++)
  {
    *ptr++ = EEPROM.read(address++);
  }
  return address;
}