-5

我在尝试将 uint64_t 大小的数据保存到数组中的 4 个 uint16_t 位置而不使用任何循环时遇到问题...这是我的代码的一部分:

static int send(uint16_t addr, const void *data)
{
    uint16_t frame[7];
    /* Here I want to save in frame[2], frame[3], frame[4] and frame[5] the data recieved by parameter */

}

提前致谢!:)

4

1 回答 1

0
typedef union
{
    uint64_t u64;
    uint32_t u32[2];
    uint16_t u16[4];
    uint8_t u8[8];
}UINT_UNION_T;

uint16_t *saveU64(uint16_t *table, size_t position, uint64_t value)
{
    UINT_UNION_T u = {.u64 = value};

    table[position] = u.u16[0];
    table[position + 1] = u.u16[1];
    table[position + 2] = u.u16[2];
    table[position + 3] = u.u16[3];
    return table;
}

或者

  memcpy(table + position, &value, sizeof value);
于 2018-05-23T09:16:52.020 回答