Function of wildcards in variable declarations - const byte* p

Looking into using EEPROM, I came across this forum thread from 2017, where @zhomeslice shared his code for reading and writing structs to EEPROM.

In the code, what is the use/function of the wildcards used in the variable declarations?

ex:

byte* p = (byte*)(void*)&value;

I’ve been looking though C documentation, but I think I just don’t know what to look for.
Full code is below.

//EEPROMAnything.h

#include <EEPROM.h>
#include <Arduino.h>  // for type definitions

template <class T> int EEPROM_writeAnything(int ee, const T& value)
{
    const byte* p = (const byte*)(const void*)&value;
    unsigned int i;
    for (i = 0; i < sizeof(value); i++)
	  EEPROM.write(ee++, *p++);
    return i;
}

template <class T> int EEPROM_readAnything(int ee, T& value)
{
    byte* p = (byte*)(void*)&value;
    unsigned int i;
    for (i = 0; i < sizeof(value); i++)
	  *p++ = EEPROM.read(ee++);
    return i;
}
struct config_t {
  double Setpoint;
  double consKp;
  double consKi;
  double consKd;
  int MinPower;
  byte Saved;
} configuration;

void EEPROMLoad() {
  EEPROM_readAnything(0, configuration);
  if (configuration.Saved != 111)return;
  SPZero = configuration.Setpoint;
  consKp = configuration.consKp;
  consKi = configuration.consKi;
  consKd = configuration.consKd;
  MinPower = configuration.MinPower;
  // Setpoint = 0.00;
}

void EEPROMSave() {
  configuration.Setpoint = SPZero;
  configuration.consKp = consKp;
  configuration.consKi = consKi;
  configuration.consKd = consKd;
  configuration.MinPower = MinPower;
  configuration.Saved = 111;
  EEPROM_writeAnything(0, configuration);

}

What you call a wildcard denotes a pointer or dereference operator

Read this

If by "wildcard" you mean the asterisk "*", that indicates that the variable is pointer to another variable of the given type.

A pointer contains the memory address of a variable.

Understood. Thank you both very much.

The line "byte* p = (byte*)(void*)&value;" is about casting the address of the variable "value" to an untyped pointer and then a typed byte pointer. Looks noobish because the "(void*)" part is not needed.

byte value = 123;
byte x = value; //Copy value to x
void* p = &value; //Set untyped pointer p to point to the address of value
byte* b = (byte*)&value;  //Set typed pointer b to point to the address of value
x = *((byte*)p); //Dereference untyped pointer p as a pointer to a byte and store value in x
x = *b; //Dereference typed pointer p as a pointer to a byte and store value in x

Easy! :wink:

Danois90:
The line "byte* p = (byte*)(void*)&value;" is about casting the address of the variable "value" to an untyped pointer and then a typed byte pointer. Looks noobish because the "(void*)" part is not needed.

byte value = 123;

byte x = value; //Copy value to x
void* p = &value; //Set untyped pointer p to point to the address of value
byte* b = (byte*)&value;  //Set typed pointer b to point to the address of value
x = ((byte)p); //Dereference untyped pointer p as a pointer to a byte and store value in x
x = *b; //Dereference typed pointer p as a pointer to a byte and store value in x




Easy! ;)

Wow, this is incredibly helpful. Thank you.

Nowadays, you can use EEPROM.put and EEPROM.get to store/retrieve anything :wink: