Function parameter eeprom_write_block

Asked

Viewed 46 times

0

Hello! I was performing some tests using the library avr/eeprom.h in my Arduino. One of its functions is to eeprom_write_block(x, y, z), writing a memory block on the EEPROM. The first parameter (x) is the variable I want to write, the second (y) is the byte of the EEPROM where I will start writing and the third (z) is the number of bytes I will write. However, I have seen in some places the following variation: eeprom_write_block(&x, y, z). I tested this variation a few times: in some cases it works with or without the &, but others only work if you have &. Could anyone clarify what difference the & brings to the function and at what times to use it?

Grateful

1 answer

0

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 &.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.