The function eeprom_write_block()
receives three parameters: the memory address from which the data will be copied, the memory address to which the data will be copied and the size of the data to be copied. As the origin of the data must be the address of a variable (of any kind -- a struct
, int
, array of char
, etc) you must pass the pointer to the variable to be copied.
Knowing this, you should understand when to use the operator &
or not. The C language is quite complex and for you to understand this subject completely we would need many pages, so I will pass a simple rule that should address your initial doubt and work for most cases, but if you intend to continue programming Arduino strongly recommend reading a book on C language. I think my answer will create new doubts in your head and to solve them you have to understand the language better, but come on.
You must use the operator &
when you need to get the pointer for a variable and should not use it when the variable itself is a pointer or array. Example:
struct Xpto {
// declaração qualquer
};
// Variáveis normais
char c;
int i;
float f;
double d;
Xpto xpto;
eeprom_write_block(&c, sizeof(c), x);
eeprom_write_block(&i, sizeof(i), x);
eeprom_write_block(&f, sizeof(f), x);
eeprom_write_block(&d, sizeof(d), x);
eeprom_write_block(&xpto, sizeof(xpto), x);
// Variáveis do tipo ponteiro
char *pc = &c; // ponteiro para char
int *pi = &i; // ponteiro para int
float *pf = &f; // ponteiro para float
double *pd = &d; // ponteiro para double
Xpto *pxpto = &xpto; // ponteiro para estrutura Xpto
char *foobarString = "foobar"; // string
int numerosPrimos[100]; // array de int
float coordenadas[3]; // array de float
eeprom_write_block(pc, sizeof(c), x);
eeprom_write_block(pi, sizeof(i), x);
eeprom_write_block(pf, sizeof(f), x);
eeprom_write_block(pd, sizeof(d), x);
eeprom_write_block(pxpto, sizeof(xpto), x);
eeprom_write_block(foobarString, strlen(foobarString), x);
eeprom_write_block(numerosPrimos, sizeof(numerosPrimos), x);
eeprom_write_block(coordenadas, sizeof(coordenadas), x);
PS: You should not read values of uninitialized variables, so you should initialize them before passing them to the function eeprom_write_block()
.
PS2: The calls strings, are nothing more than pointers to char
and must be passed without the operator &
.