Search code examples
cpointersembeddedmemory-addressflags

What are for square brackets in an addressed-of pointer?


I've been searching for a while what are for square-brackets in an addressed-of pointer, but I continue without understanding it. Here are the lines of the function, where "id" variable is an uint32_t pointer that has been previously passed as an argument.

#define LIST_STARTED (0x0001) /*defined out of the function*/
#define LIST_FIRST   (0x0002) /*defined out of the function*/

uint32_t *pointer = id;
uint16_t *flags = &((uint16_t *)pointer)[0];
uint16_t *index = &((uint16_t *)pointer)[1];
bool started = *flags & LIST_STARTED;
bool first   = *flags & LIST_FIRST;

if (!started){
    /* does something */
    *flags = MSEC_PRM_MGMT_LIST_STARTED;
    return true;
}
if (!first){
    /* does something */
    *flags |= MSEC_PRM_MGMT_LIST_FIRST;
    *index = 1;
    return true;
}
if (*index == final_index)
    /* does something */
    return false;

*index += 1;

I understand what the logic of the function is, but I don't understand what do the following lines. I put "all" the code above in case it helps you.

uint16_t *flags = &((uint16_t *)pointer)[0];
uint16_t *index = &((uint16_t *)pointer)[1];

I would appreciate if someone could help me! Thank you!

I edit to say that this C code works fine in an Embedded System, I'm not modifying it, I was just watching its behaviour.


Solution

  • The following code tries to read a uint32_t object as an array of two uint16_t objects:

    uint32_t *id = ...;
    uint32_t *pointer = id;
    uint16_t *flags = &((uint16_t *)pointer)[0];
    uint16_t *index = &((uint16_t *)pointer)[1];
    

    and that is undefined behaviour to read a uint32_t object as 2 uint16_t objects through flags and index pointers because that breaks strict aliasing rules.

    The correct way is:

    uint16_t flags = *id; // The lowest order bits of id.
    uint16_t index = *id >> 16; // The highest order bits of id.
    

    In the above assignments of uint32_t to uint16_t it truncates the highest order bits of id.