Why are different pointers and void pointers used by these functions?

I have ATMega328, and I use functions <avr/eeprom.h>to use inline EEPROM.

I can use it correctly EEPROM, but I do not understand the arguments of the function that I pass in the function EEPROM.

For example, to write different types of data, I can use

void eeprom_update_byte (uint8_t *addr, uint8_t value);
void eeprom_update_word (uint16_t *addr, uint16_t value);
void eeprom_update_dword (uint32_t *addr, uint32_t value);
void eeprom_update_float (float *addr, float value);
  • But why does the type of pointer for the address (parameter addr) change depending on the function used? If it addrsimply indicates a valid address EEPROM, why is the type in each function different?

  • Also, using the function void *in the function EEPROMbelow is confusing to me. I understand that it void *can point to any address, so I assume that the function simply writes bytes bytes to the data in src, but I'm not sure if this is correct?

void eeprom_update_block (const void *src, void *dst, size_t n);

+4
source share
2 answers

The first four functions listed determine the type of object being recorded. Thus, an internal function might look like this:

void eeprom_update_float (float *addr, float value)
{
    *addr = value;
}

This offers the benefits of type safety (for example, cannot write floatto uint16_t), ensuring that the destination pointer matches the source variable.

void eeprom_update_block() . , - memcpy :

void eeprom_update_block (const void *src, void *dst, size_t n)
{
    memcpy(dst, src, n);
}

- , . , ​​:

eeprom_update_block(&myStruct, dst, sizeof(myStruct));
+4

API , . void* , :

void eeprom_update_byte (void *addr, uint8_t value);
void eeprom_update_word (void *addr, uint16_t value);
void eeprom_update_dword (void *addr, uint32_t value);
void eeprom_update_float (void *addr, float value);

EEPROM , , Atmel .

, void* EEPROM .

eeprom_update_block - eeprom_update_XXX. , void*, , , eeprom_update_XXX .

+1

Source: https://habr.com/ru/post/1662853/


All Articles