Arduino passing an array to a function

Hello. I am writing my own EEPROM wrapper for ESP32. I want to implement wear leveling and write data in chunks. I have created a .c and .h files for my library and implement a eeprom_rotate class where I have all my methods.

I want to pass an array of data to a function and then the function would put all the data in the EEPROM. Please see the following

// size of eeprom must be dividable by chunk_size
eeprom_rotate::eeprom_rotate(size_t size_of_eeprom, size_t chunk_size) {
    EEPROM.begin(size_of_eeprom);
    Serial.println("the class constructor has been created");
    this->size_of_eeprom = size_of_eeprom;
    this->chunk_size = chunk_size;
}

void eeprom_rotate::write_to_chunk(size_t chunk, uint8_t* data_to_write, size_t size_of_array){
  EEPROM.put(chunk,data_to_write);
    Serial.println("printing after put");
    for(int i = 0; i<size_of_eeprom;i++){
      Serial.println(EEPROM.read(i));
    }
}

I am callings functions in my main arduino .ino file as following:


  eeprom_rotate eeprom1(50,10);
  uint8_t data_buffer[10] = {1,2,3,4,5,6,7,8,9,10};
  Serial.print("size of data buffer =");
  Serial.println(sizeof(data_buffer));
  eeprom1.write_to_chunk(0,data_buffer,10);

But the result is not as expected:


printing after put
130
31
251
63
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255
255

However, if I create an array in my eeprom_write_chunk method , it works as expected which proves that EEPROM is working fine.


void eeprom_rotate::write_to_chunk(size_t chunk, uint8_t* data_to_write, size_t size_of_array){
//create array inside here
int array1[10] = {1,2,3,4,5,6,7,8,9,10};
  EEPROM.put(chunk, array1);
    Serial.println("printing after put");
    for(int i = 0; i<size_of_eeprom;i++){
      Serial.println(EEPROM.read(i));
    }
}

Please can someone help me understand what is happening that causes that?

aren't the arguments to put() the address and size?

looks like your code is specifying size and address

in the first case you are passing a pointer to .put(), in the second case the object, try passing your array by reference to the function

the arguments to put should be : the address and the data to send. This is what i am passing right? The chunk is the starting address that I want to write to and data_to_write is my array of data

isn't chunk the size of the data? isn't it the first arg to put()?
shouldn't data_to_write be the first arg?

Are you suggesting something like eeprom1.write_to_chunk(0,&data_buffer,10); ? . If that is the case, i am getting an error returned :

 /Users/Lukas/Documents/Arduino/libraries/eeprom_rotate/eeprom_rotate.h:21:9: note:   no known conversion for argument 2 from 'uint8_t (*)[10] {aka unsigned char (*)[10]}' to 'uint8_t* {aka unsigned char*}'
exit status 1
no matching function for call to 'eeprom_rotate::write_to_chunk(int, uint8_t (*)[10], int)'

No. It is confusing name sorry. I divide my EEPROM memory into chunks. For example chunk 0 will always mean the address 0, chunk 1 will mean the address 10 and etc. Because I declared the size of my chunk as 10. So all I am trying to do is to write 10 values starting from address 0 into my EEPROM

chunk isn't an address

Please clarify to me what do you mean. Simply replace the "chunk" with 0 and you will have the same behaviour. The starting address writing EEPROM.put will be 0.

Definitely not. Calling is fine, it is the function arguments that need to be changed, try

void eeprom_rotate::write_to_chunk(size_t chunk, int (&data_to_write)[10], size_t size_of_array)

Hello,

You can't use put with a pointer to an array, obviously it would need to know the size of the array to know when to stop writing. It works in your second example because the array is directly available and is passed to put as an array of 10 ints, because put is a templated function... If that makes sense.

This seems to be working! Thank you very much. I do not have much experience in programming but from what I have learned before, passing the parameters as a pointer or as a reference usually is the same.

not with arrays

Could you please help me understand what is the difference passing my array as a pointer or passing it as a reference in this particular case?

Array decays to a pointer to its first element, the size information is lost, you know where it starts but not where it ends, so this is why when you see array passed to a function it almost always contains size argument in addition. When you pass by reference, it preserves array information including size (which is why you have to explicitly indicate it). I am not sure why .put() doesnt have overload to take array pointer and size, it would have been ok for your usage

Understood thank you very much

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.