Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        2年前关闭。
                                                                                            
                
        
我在尝试将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 */

}


提前致谢! :)

最佳答案

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);

关于c - 用C中的uint64_t数据填充uint16_t数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50484021/

10-12 13:31