简体   繁体   English

uint8_t数组的数组

[英]Array of uint8_t arrays

I have four uint8_t arrays: 我有四个uint8_t数组:

uint8_t arrayOne[12]   = { 0x00,0x01,0x00,0x00,0x00,0x06,0xFE,0x03,0x01,0xC1,0x00,0x01 };

uint8_t arrayTwo[12]   = { 0x00,0x01,0x00,0x00,0x00,0x06,0xFE,0x03,0x4E,0x2D,0x00,0x0C };

uint8_t arrayThree[12] = { 0x00,0x01,0x00,0x00,0x00,0x06,0xFE,0x03,0x01,0xF3,0x00,0x01 };

uint8_t arrayFour[12]  = { 0x00,0x01,0x00,0x00,0x00,0x06,0xFE,0x03,0x20,0x04,0x00,0x01 };

and I want them to add to another array: 我希望他们添加到另一个数组:

uint8_t theArray[4][12] = { arrayOne,arrayTwo,arrayThree,arrayFour };

but the values of arrays change when I add them to theArray. 但是当我将它们添加到theArray时,数组的值会发生变化。

Why? 为什么? How could I add them to array properly? 我怎么能正确地将它们添加到阵列?

The toplevel array should just be an array of pointers: toplevel数组应该只是一个指针数组:

uint8_t *theArrays[] = { arrayOne,arrayTwo,arrayThree,arrayFour };

You will lose information about the lengths of each "row", but that's fine. 您将丢失有关每个“行”长度的信息,但这没关系。

I don't think you can reference an array in the initializer like in your example and have the elements copied into a larger array automatically. 我不认为你可以像在你的例子中那样在初始化器中引用一个数组,并自动将元素复制到一个更大的数组中。

You have to copy each array in the result array using standard function memcpy declared in header <string.h> . 您必须使用在头文件<string.h>声明的标准函数memcpy复制结果数组中的每个数组。 For example 例如

#include <string.h>
#include <stdint.h>

//...

uint8_t * arrayPtrs[] = { arrayOne, arrayTwo, arrayThree, arrayFour };

for ( size_t i = 0; i < sizeof( theArray ) / sizeof( *theArray ); i++ )
{
    memcpy( theArray[i], arrayPtrs[i], sizeof( theArray[i] ) );
}

You are trying to initialize an uint8_t array with pointers to uint8_t , not uint8_t elements. 您正在尝试使用指向uint8_t的指针初始化uint8_t数组 ,而不是uint8_t元素。

try this: 尝试这个:

uint8_t *theArrays[] = { array1,array2, ... };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM