我们如何将数组的struct指针重新加载回uint8_t数组?

时间:2014-01-27 01:48:51

标签: c arrays struct

给定结构

struct media{
     uint32_t addressOfPtr; 
 };

并在main()

uint8_t *msg_Ptr = NULL;

//之后已经在msg_Ptr

中填充了给定数据
for (i=0; i < SIZE_MAX; i++)  // message fixed at length 10
    printf(" > %d",  msg_Ptr[i]);


struct media mediaObject2;
(mediaObject2.addressOfPtr) = malloc(SIZE_MAX*sizeof(uint8_t));
(mediaObject2.addressOfPtr) = (uint32_t) msg_Ptr;

functionB (&mediaObject2);

//我们如何在数组缓冲区uint8_t消息[SIZE_MAX]中重新加载它?

void functionB (void* mediaObject) {

int i = 0;
// designate the structure / form of data to be loaded onto
uint8_t message[SIZE_MAX];
uint8_t* message_Ptr;

message_Ptr = (uint8_t*) media_ptr->addressOfPtr; // ->addressOfPtr

printf("Data that arrived in function B: \n");
for (i=0; i < SIZE_MAX; i++)  // message fixed at length 10
    printf(" %x",  ((uint8_t*) media_ptr->addressOfPtr)[i]);

printf("\n");

    // how to load back in uint8_t message[SIZE_MAX] ?


}

1 个答案:

答案 0 :(得分:0)

我正在尝试memcpy,但这是一种有效的方法。

for (i=0; i < SIZE_MAX; i++)  {// message fixed at length 10
    //printf(" %x",  ((uint8_t*) media_ptr->addressOfPtr)[i]);
    message[i] = ((uint8_t*) media_ptr->addressOfPtr)[i];
    printf(" %x",  message[i]);
}