# EEPROM Writing with Functions

**URL:** <https://forum.arduino.cc/t/eeprom-writing-with-functions/1307979>\
**Category:** Storage\
**Created:** [October 4, 2024, 1:55pm UTC](https://forum.arduino.cc/t/eeprom-writing-with-functions/1307979 "2024-10-04T13:55:04Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![malklor](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/malklor/32/1109424_2.png) [@malklor](https://forum.arduino.cc/u/malklor)\
**Post date:** [October 4, 2024, 1:55pm UTC](https://forum.arduino.cc/t/eeprom-writing-with-functions/1307979/1 "2024-10-04T13:55:04Z")

</div>

I am new to C++ and Arduino coding and I am struggling to figure out why one version of my code works but the other doesn't

I am trying to do simple test of writing a defined array of custom structure to EEPROM then reading it back again. (This is part of game I am writing but I have split this out to debug)

In this code version of the code I apply the EEPROM.put and EEPROM.get all in the setup function and it works as expected.

```arduino
#include <EEPROM.h>

#define DEBUG 1;

struct highscore {
  byte attempts;
  char name[4];
};

struct highscore highScores[5];

struct highscore dftHighScores[5] = {
  {10, "AAA"},
  {20, "BBB"},
  {30, "CCC"},
  {40, "DDD"},
  {50, "EEE"}
};

int initFlag = 0;
int eeAddressFlag = 0;

void setup() {

  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  EEPROM.get(eeAddressFlag, initFlag);
  
  #ifdef DEBUG
    Serial.print(" flag = ");
    Serial.println(initFlag);
  #endif
  
  if (initFlag != 5) {
    
    EEPROM.put(eeAddressFlag, 5);
    
    // Setup Default High Scores
    Serial.println("Setup Default HighScores");
    EEPROM.put(10, dftHighScores);
    
  }

  //Load High Scoores
  EEPROM.get(10, highScores);

  #ifdef DEBUG
    for(int i=0; i<5; i++) {
      HighScoreLog(highScores[i]);
      Serial.println("======================");
    }
  #endif
}

void HighScoreLog(struct highscore temp)
{
  Serial.println();
  Serial.print(" Name = ");
  Serial.println(temp.name);
  Serial.print(" Attempts = ");
  Serial.println(temp.attempts);
}

// }

void loop() {

  /* Empty loop */
}

```

But if I change the code so that the EEPROM.put is in its own function then my read seems to get bad data and I cannot figure out why

```arduino
#include <EEPROM.h>

#define DEBUG 1;

struct highscore {
  byte attempts;
  char name[4];
};

struct highscore highScores[5];

struct highscore dftHighScores[5] = {
  {10, "AAA"},
  {20, "BBB"},
  {30, "CCC"},
  {40, "DDD"},
  {50, "EEE"}
};

int initFlag = 0;
int eeAddressFlag = 0;

void setup() {

  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  EEPROM.get(eeAddressFlag, initFlag);
  
  #ifdef DEBUG
    Serial.print(" flag = ");
    Serial.println(initFlag);
  #endif
  
  if (initFlag != 5) {
    
    EEPROM.put(eeAddressFlag, 5);
    
    // Setup Default High Scores
    Serial.println("Setup Default HighScores");
    writeHighScore(dftHighScores);
    
  }

  //Load High Scoores
  EEPROM.get(10, highScores);

  #ifdef DEBUG
    for(int i=0; i<5; i++) {
      HighScoreLog(highScores[i]);
      Serial.println("======================");
    }
  #endif
}

void writeHighScore(highscore writeScore[5])
{
  #ifdef DEBUG
      for(int i=0; i<5; i++) {
        HighScoreLog(writeScore[i]);
        Serial.println("+++++++++++++++++++++++");
      }
  #endif
  
  EEPROM.put(10, writeScore);
  delay(250);
}

void HighScoreLog(struct highscore temp)
{
  Serial.println();
  Serial.print(" Name = ");
  Serial.println(temp.name);
  Serial.print(" Attempts = ");
  Serial.println(temp.attempts);
}

void loop() {

  /* Empty loop */
}

```

Output is now as follows:  
flag = -1

Setup Default HighScores

Name = AAA

Attempts = 10

+++++++++++++++++++++++

Name = BBB

Attempts = 20

+++++++++++++++++++++++

Name = CCC

Attempts = 30

+++++++++++++++++++++++

Name = DDD

Attempts = 40

+++++++++++++++++++++++

Name = EEE

Attempts = 50

+++++++++++++++++++++++

Name = ÿÿÿ

Attempts = 0

======================

Name = ÿÿÿÿ

Attempts = 255

======================

Name = ÿÿÿÿ

Attempts = 255

======================

Name = ÿÿÿÿ

Attempts = 255

======================

Name = ÿÿÿÿ

Attempts = 255

======================

Any help would be appreciated I am sure it is something simple I am missing.

---

<div class="post-metadata">

**Author:** ![kolaha](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kolaha/32/966427_2.png) [@kolaha](https://forum.arduino.cc/u/kolaha)\
**Post date:** [October 4, 2024, 2:18pm UTC](https://forum.arduino.cc/t/eeprom-writing-with-functions/1307979/2 "2024-10-04T14:18:14Z")

</div>

did you tried it with real board?

---

<div class="post-metadata">

**Author:** ![malklor](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/malklor/32/1109424_2.png) [@malklor](https://forum.arduino.cc/u/malklor)\
**Post date:** [October 4, 2024, 2:29pm UTC](https://forum.arduino.cc/t/eeprom-writing-with-functions/1307979/3 "2024-10-04T14:29:24Z")

</div>

no I have been using

> **[Wokwi - World's most advanced ESP32 Simulator](https://wokwi.com/)**
>
> IoT and Embedded System Simulator: ESP32, STM32, Arduino, Raspberry Pi Pico, displays, sensors, motors and WiFi simulation.

to simulate everything but so far behavior has been the same on both sides

---

<div class="post-metadata">

**Author:** ![malklor](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/malklor/32/1109424_2.png) [@malklor](https://forum.arduino.cc/u/malklor)\
**Post date:** [October 4, 2024, 2:33pm UTC](https://forum.arduino.cc/t/eeprom-writing-with-functions/1307979/4 "2024-10-04T14:33:45Z")

</div>

Just tested the same code on a actual board and I got the same behavior

09:33:13.480 -\> flag = 5

09:33:13.480 -\> Setup Default HighScores

09:33:13.516 -\>

09:33:13.516 -\> Name = AAA

09:33:13.516 -\> Attempts = 10

09:33:13.516 -\> +++++++++++++++++++++++

09:33:13.516 -\>

09:33:13.516 -\> Name = BBB

09:33:13.550 -\> Attempts = 20

09:33:13.550 -\> +++++++++++++++++++++++

09:33:13.583 -\>

09:33:13.583 -\> Name = CCC

09:33:13.616 -\> Attempts = 30

09:33:13.616 -\> +++++++++++++++++++++++

09:33:13.683 -\>

09:33:13.683 -\> Name = DDD

09:33:13.683 -\> Attempts = 40

09:33:13.683 -\> +++++++++++++++++++++++

09:33:13.716 -\>

09:33:13.716 -\> Name = EEE

09:33:13.716 -\> Attempts = 50

09:33:13.748 -\> +++++++++++++++++++++++

09:33:13.962 -\>

09:33:13.962 -\> Name =

09:33:13.962 -\> Attempts = 0

09:33:13.962 -\> ======================

09:33:13.995 -\>

09:33:13.995 -\> Name = ����

09:33:14.029 -\> Attempts = 1

09:33:14.029 -\> ======================

09:33:14.062 -\>

09:33:14.062 -\> Name = ����

09:33:14.062 -\> Attempts = 255

09:33:14.095 -\> ======================

09:33:14.128 -\>

09:33:14.128 -\> Name = ����

09:33:14.128 -\> Attempts = 255

09:33:14.162 -\> ======================

09:33:14.162 -\>

09:33:14.195 -\> Name = ����

09:33:14.195 -\> Attempts = 255

09:33:14.195 -\> =====================

---

<div class="post-metadata">

**Author:** ![kolaha](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kolaha/32/966427_2.png) [@kolaha](https://forum.arduino.cc/u/kolaha)\
**Post date:** [October 4, 2024, 2:38pm UTC](https://forum.arduino.cc/t/eeprom-writing-with-functions/1307979/5 "2024-10-04T14:38:21Z")

</div>

no, it works. try so:

```arduino
#include <EEPROM.h>

struct highscore {
  byte attempts;
  char name[4];
};

highscore HS[5];
highscore dftHighScores[5] = {
  {10, "AAA"},
  {20, "BBB"},
  {30, "CCC"},
  {40, "DDD"},
  {50, "EEE"}
};

void setup() {
  Serial.begin(9600);
  writeHighScore();
}

void writeHighScore()
{
  EEPROM.put(10, dftHighScores);
  EEPROM.get(10, HS);
  Serial.println();
  for (int i = 0; i < 5; i++) {
    Serial.print(" Name = ");
    Serial.println(HS[i].name);
    Serial.print(" Attempts = ");
    Serial.println(HS[i].attempts);
    Serial.println("+++++++++++++++++++++++");
  }
}

void loop() {}

```

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [October 4, 2024, 2:51pm UTC](https://forum.arduino.cc/t/eeprom-writing-with-functions/1307979/6 "2024-10-04T14:51:01Z")

</div>

Both programs work as expected for me, on an Arduino Uno.

---

<div class="post-metadata">

**Author:** ![Dave\_Lowther](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dave_lowther/32/1092885_2.png) [@Dave\_Lowther](https://forum.arduino.cc/u/Dave_Lowther)\
**Post date:** [October 4, 2024, 3:03pm UTC](https://forum.arduino.cc/t/eeprom-writing-with-functions/1307979/7 "2024-10-04T15:03:24Z")

</div>

Same for me.

---

<div class="post-metadata">

**Author:** ![Dave\_Lowther](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/dave_lowther/32/1092885_2.png) [@Dave\_Lowther](https://forum.arduino.cc/u/Dave_Lowther)\
**Post date:** [October 4, 2024, 3:12pm UTC](https://forum.arduino.cc/t/eeprom-writing-with-functions/1307979/8 "2024-10-04T15:12:48Z")

</div>

> [@malklor](#):
>
> Just tested the same code on a actual board and I got the same behavior

What exactly are you doing?  
What I did was built / uploaded the working version from post #1 and got the correct output.  
Then I built / uploaded the not working version from post #1 and got the same correct output.  
We must be doing something different. I'm using an Uno R3.

---

<div class="post-metadata">

**Author:** ![malklor](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/malklor/32/1109424_2.png) [@malklor](https://forum.arduino.cc/u/malklor)\
**Post date:** [October 4, 2024, 3:26pm UTC](https://forum.arduino.cc/t/eeprom-writing-with-functions/1307979/9 "2024-10-04T15:26:39Z")

</div>

I understand that the simulator might be off.

So I am using Arduino IDE to connect to two different boards to run my test one is this one

> **[Maker UNO: Simplifying Arduino for {Education}](https://www.cytron.io/p-maker-uno-simplifying-arduino-for-education)**
>
> Maker UNO was 1825% funded on Kickstarter on Apr 17, 2018.  Maker UNO is an Arduino Compatible board specially designed to simplify building your projects:Coding & Electronics made easy with built-in:{ 12x LEDs; 1x buzzer; 1x button;...

( Maker UNO)

the other is this one

> **[Amazon.com: ELEGOO UNO R3 Board ATmega328P with USB Cable(Arduino-Compatible)...](https://www.amazon.com/ELEGOO-Board-ATmega328P-ATMEGA16U2-Compliant/dp/B01EWOE0UU)**
>
> Amazon.com: ELEGOO UNO R3 Board ATmega328P with USB Cable(Arduino-Compatible) for Arduino : Electronics

( ELEGOO UNO R3 Board ATmega328P with USB Cable(Arduino-Compatible) for Arduino)

both run my code the same as the simulator, and the later one is a UNO R3 board (although generic version)

When I run the modified code by kolaha it all works on all environments. It seems in my case as soon as I use the write method like this

```arduino
void writeHighScore(highscore writeScore[5])
{
  EEPROM.put(10, writeScore);
  
}

```

it breaks but using it like this

```arduino
void writeHighScore()
{
 EEPROM.put(10, dftHighScores);
 
}

```

seems to work but I really want to be able to pass in the write to the function as it will be used to update highscores not just set the defaults.

---

<div class="post-metadata">

**Author:** ![kolaha](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kolaha/32/966427_2.png) [@kolaha](https://forum.arduino.cc/u/kolaha)\
**Post date:** [October 4, 2024, 4:54pm UTC](https://forum.arduino.cc/t/eeprom-writing-with-functions/1307979/10 "2024-10-04T16:54:36Z")

</div>

> [@malklor](#):
>
> ```arduino
> void writeHighScore(highscore writeScore[5])
> {
> EEPROM.put(10, writeScore);
>   
> }
> 
> ```

```arduino
void writeHighScore(highscore writeScore[5])
{
  EEPROM.put(10, writeScore);
  Serial.print(sizeof(writeScore)); // =2
  Serial.print(sizeof(dftHighScores)); // =25
}

```

---

<div class="post-metadata">

**Author:** ![malklor](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/malklor/32/1109424_2.png) [@malklor](https://forum.arduino.cc/u/malklor)\
**Post date:** [October 4, 2024, 7:23pm UTC](https://forum.arduino.cc/t/eeprom-writing-with-functions/1307979/11 "2024-10-04T19:23:58Z")

</div>

I see that on my end too but why is the big question I have

(not real code just example)

```arduino
writeHighScore(dftHighScores);

void writeHighScore(highscore writeScore[5])
{
  EEPROM.put(10, writeScore);
  Serial.println(sizeof(writeScore)); // =2
  Serial.println(sizeof(dftHighScores)); // =25
  
}

```

Since I am passing dftHighScores to writeHighScore and assigning it to writeScore why are they not the exact same size?

if I do this

```arduino
void writeHighScore(highscore writeScore[5])
{
  for (int i = 0; i < 5; i++) {
    Serial.print(" Name = ");
    Serial.println(writeScore[i].name);
    Serial.print(" Attempts = ");
    Serial.println(writeScore[i].attempts);
    Serial.println("+++++++++++++++++++++++");
  }
  
  EEPROM.put(10, writeScore);
  Serial.println(sizeof(writeScore)); // =2
  Serial.println(sizeof(dftHighScores)); // =25
  
}

```

I can see that writeScores has all the data that expect -- I am just confused as to why the sizeof would be different.

I feel like the answer to that is my problem.

Sorry if this is me being naive but I am new to this.

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [October 4, 2024, 7:33pm UTC](https://forum.arduino.cc/t/eeprom-writing-with-functions/1307979/12 "2024-10-04T19:33:24Z")

</div>

> [@malklor](#):
>
> I am just confused as to why the sizeof would be different.

AFAIK the total size of a function argument has to be communicated separately, usually using one or more separate function argument(s). If you pass an pointer or array of N elements, pass N as well.

---

<div class="post-metadata">

**Author:** ![cattledog](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cattledog/32/217816_2.png) [@cattledog](https://forum.arduino.cc/u/cattledog)\
**Post date:** [October 4, 2024, 7:44pm UTC](https://forum.arduino.cc/t/eeprom-writing-with-functions/1307979/13 "2024-10-04T19:44:05Z")

</div>

> [@malklor](#):
>
> I am just confused as to why the sizeof would be different.

I think you are seeing the size of the pointer to the data and not the size of the data block itself.

---

<div class="post-metadata">

**Author:** ![malklor](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/malklor/32/1109424_2.png) [@malklor](https://forum.arduino.cc/u/malklor)\
**Post date:** [October 4, 2024, 8:03pm UTC](https://forum.arduino.cc/t/eeprom-writing-with-functions/1307979/14 "2024-10-04T20:03:03Z")

</div>

Is it possible that this function then is just writing the point to the eeprom

```arduino
void writeHighScore(highscore writeScore[5])
{
  for (int i = 0; i < 5; i++) {
    Serial.print(" Name = ");
    Serial.println(writeScore[i].name);
    Serial.print(" Attempts = ");
    Serial.println(writeScore[i].attempts);
    Serial.println("+++++++++++++++++++++++");
  }
  
  EEPROM.put(10, writeScore);
  Serial.println(sizeof(writeScore)); // =2
  Serial.println(sizeof(dftHighScores)); // =25
  
}

```

not the actual data and that is why I am getting the different behavior. If that is the case how would I resolve it .

---

<div class="post-metadata">

**Author:** ![malklor](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/malklor/32/1109424_2.png) [@malklor](https://forum.arduino.cc/u/malklor)\
**Post date:** [October 4, 2024, 8:04pm UTC](https://forum.arduino.cc/t/eeprom-writing-with-functions/1307979/15 "2024-10-04T20:04:30Z")

</div>

would you be able to give me a code sample of how I should do this correctly then?

---

<div class="post-metadata">

**Author:** ![fregloin](https://avatars.discourse-cdn.com/v4/letter/f/e56c9b/32.png) [@fregloin](https://forum.arduino.cc/u/fregloin)\
**Post date:** [October 4, 2024, 8:08pm UTC](https://forum.arduino.cc/t/eeprom-writing-with-functions/1307979/16 "2024-10-04T20:08:18Z")

</div>

Why don't pass arrays as references or pointers?

---

<div class="post-metadata">

**Author:** ![kolaha](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kolaha/32/966427_2.png) [@kolaha](https://forum.arduino.cc/u/kolaha)\
**Post date:** [October 4, 2024, 8:29pm UTC](https://forum.arduino.cc/t/eeprom-writing-with-functions/1307979/17 "2024-10-04T20:29:00Z")

</div>

```arduino
void writeHighScore(highscore *writeScore) {//same as 'writeScore[]'
  Serial.println("+++++++++++++++++++++++");

  highscore HS[5];
  for (byte i = 0; i < 5; i++) {
    HS[i] = writeScore[i];
    HighScoreLog(HS[i]);
  }
 
  EEPROM.put(10, HS);
  Serial.println("+++++++++++++++++++++++");
}

```

OR

```arduino
void writeHighScore(highscore writeScore[]) {
  Serial.println("+++++++++++++++++++++++");

  for (byte i = 0; i < 5; i++) {
    EEPROM.put(10 + i * sizeof(highscore), writeScore[i]);
    HighScoreLog(writeScore[i]);
  }

  Serial.println("+++++++++++++++++++++++");
}

```

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [October 4, 2024, 8:37pm UTC](https://forum.arduino.cc/t/eeprom-writing-with-functions/1307979/18 "2024-10-04T20:37:36Z")

</div>

One of many tutorials on the topic. There are several other approaches to communicating sizes.

> **[C++ Passing Arrays as Function Parameters (With Examples)](https://www.programiz.com/cpp-programming/passing-arrays-function)**
>
> In this tutorial, we will learn how to pass a single-dimensional and multidimensional array as a function parameter in C++ with the help of examples.

---

<div class="post-metadata">

**Author:** ![kolaha](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/kolaha/32/966427_2.png) [@kolaha](https://forum.arduino.cc/u/kolaha)\
**Post date:** [October 4, 2024, 9:11pm UTC](https://forum.arduino.cc/t/eeprom-writing-with-functions/1307979/19 "2024-10-04T21:11:39Z")

</div>

how should EEPROM.put() look to use whole passed array?

```arduino
void writeHighScore(highscore writeScore[5]){
  EEPROM.put(10, writeScore);
}

```

not work, it is clear.

* * *

```arduino
void writeHighScore(highscore writeScore[5]){
  EEPROM.put(10, *writeScore);
}

```

work, but written is only first structure from 5

* * *

---

<div class="post-metadata">

**Author:** ![malklor](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/malklor/32/1109424_2.png) [@malklor](https://forum.arduino.cc/u/malklor)\
**Post date:** [October 4, 2024, 11:14pm UTC](https://forum.arduino.cc/t/eeprom-writing-with-functions/1307979/20 "2024-10-04T23:14:38Z")

</div>

Thank you everyone I learn allot here and found my solution to work. Bonus is I understand what I did wrong and why the previous didn't work.

I am not sure why other were able to get the original code to work on there system but I couldn't that still has be baffled less concerning of all of it

Thanks @kolaha for the great code samples  
and @anon94801460 great link with some good information that helps me learn more.

Thanks again and I sure I will be back!

[Next page](https://forum.arduino.cc/t/eeprom-writing-with-functions/1307979.md?page=2)
