Help me! (uint8_t *) &var

Sy i'm a beginner, but i didn't understand what means this variable statement

 (uint8_t*)  &var

thx in advance
NP

1 Like

&var is address of var variable and (uint8_t*) is a cast to type uint8_t* pointer from any it means that the result of this statement is address of uint8_t (same as unsigned char - 1 byte).

"var" is a variable of unknown type and size. It is stored in memory at address &var. To access that memory with a different type and size of variable you can cast it to a different pointer type, "uint8_t *" in this case.

For instance, if "var" is an unsigned integer "2969", and the system stores them "little endian" in memory, then in the following code:

unsigned int var = 2969;  // Assign initial value
uint8_t *ptr;  // Create pointer variable
ptr = (uint8_t *) &var;  // Point variable at first memory location
uint8_t out = *ptr;  // Get contents from memory address pointed to by pointer

"out" would end up containing 153, since the value 2969 is made up of two bytes, 153 and 11 (11 * 256 + 153 = 2969), and 153 is the "low order" byte of the two that make up the integer value.