About type casting in FreeRTOS Tutorial example 14

hi,

I’ve been studying FreeRTOS using Tutorial.

there’s API function about Timer ID in Software Timer chapter.

void *pvTimerGetTImerID( TimerHnadler_t xTimer);

this function’s type is (void *) so I believe if I wanna type cast this function’s return value to uint32_t, it would be

uint32_t val = (uint32_t *)pvTimerGetTimerID(xTimer);

but according to example 14

static void prvTimerCallback( TimerHandle_t xTimer )
{
TickType_t xTimeNow;
uint32_t ulExecutionCount;

...
 ulExecutionCount = ( uint32_t ) pvTimerGetTimerID( xTimer );
 ulExecutionCount++;
...

it is just ( uint32_t ) not ( uint32_t * ). and it’s working well.

my question is how it’s possible.
if there is something I’ve missed please let me know.

thank you so much.

If you want to cast any value to uint32_t, it needs to be ( uint32_t ), regardless of the original type:

uint8_t val1 = 0;
uint16_t val2 = 0;

uint32_t casted_val1 = ( uint32_t ) val1; /* Cast uint8_t to uint32_t. */
uint32_t casted_val2 = ( uint32_t ) val2; /* Cast uint16_t to uint32_t. */

uint32_t * pu32;
void * pv32;

uint32_t casted_pu32 = ( uint32_t ) pu32; /* Cast uint32_t* (i.e. pointer to uint32_t) to uint32_t. */
uint32_t casted_pv32 = ( uint32_t ) pv32; /* Cast void* (i.e. pointer to void) to uint32_t. */

1 Like