假设我有两个结构。一个结构体是具有一组元素的简单结构体。
typedef struct __attribute__ ((packed)) {
float a1;
float a2;
uint32_t b1;
uint32_t b2;
} item;
其他结构只是先前结构item
的数组。
typedef struct __attribute__ ((packed)) {
item item_queue[65000];
} item_arr;
我需要做的是从 item_arr 获取一个元素并将其分配给 item
数组。我正在尝试以这种方式实现它。
item_arr profile_arr[16] = {0};
为 profile_arr 分配值后,我将 item_arr 中的一个元素分配给新的项目数组。
item temp_q[65000] = profile_arr[0].item_queue;
但这给了我错误:无效的初始化程序。
我是否以错误的方式进行初始化?任何帮助将不胜感激。
最佳答案
如果您只想将 profile_arr[0].item_queue
复制到 temp_q
,则可以使用 memcpy
.
void * memcpy ( void * destination, const void * source, size_t num );
memcpy(temp_q, profile_arr[0].item_queue, sizeof(temp_q));
关于c - 无效的初始化程序 : While assigning value from struct to a struct array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53076265/