在Contiki中发送和接收结构-处理指针和结构
我现在正在制作一个简单的程序,它试图使用contiki通过网络发送一个结构。我有一个函数,在接收传输时调用,还有一个函数调用,用于传输 结构的定义如下所示:在Contiki中发送和接收结构-处理指针和结构,c,pointers,contiki,C,Pointers,Contiki,我现在正在制作一个简单的程序,它试图使用contiki通过网络发送一个结构。我有一个函数,在接收传输时调用,还有一个函数调用,用于传输 结构的定义如下所示: struct metadata { int values[5]; }; struct metadata packet; int max_val = 100; int min_val = 1; int random_val = random_rand(); if(random_val < 0) { rand
struct metadata {
int values[5];
};
struct metadata packet;
int max_val = 100;
int min_val = 1;
int random_val = random_rand();
if(random_val < 0)
{
random_val *= -1;
}
int proposal_value = (random_val % max_val) + min_val;
int index = node_id - 1;
packet.values[index] = proposal_value;
以下代码用于tcpip\u处理程序
函数,该函数在远程接收到传输时调用。
static void trickle_tx(void *ptr, uint8_t suppress)
{
uip_ipaddr_copy(&trickle_conn->ripaddr, &ipaddr);
uip_udp_packet_send(trickle_conn, &packet, sizeof(packet));
/* Restore to 'accept incoming from any IP' */
uip_create_unspecified(&trickle_conn->ripaddr);
leds_off(LEDS_RED);
}
据我所知,我正在发送&packet
,这是我的数据包结构的内存地址。在接收时,我希望数据在这个结构中,因此首先需要访问内存位置。因此,我创建了一个类型为struct metadata
的变量received\u struct
,如下所示:第一种方法:
if(uip_newdata()) {
struct metadata received_struct;
received_struct = (struct metadata) *uip_appdata;
int data[5];
data = received_struct.values;
}
proposer.c:120:31: error: invalid use of void expression
proposer.c:123:10: error: incompatible types when assigning to type ‘int[5]’ from type ‘int *
proposer.c:125:10: error: incompatible types when assigning to type ‘int[5]’ from type ‘int (*)[5]’
错误:
if(uip_newdata()) {
struct metadata received_struct;
received_struct = (struct metadata) *uip_appdata;
int data[5];
data = received_struct.values;
}
proposer.c:120:31: error: invalid use of void expression
proposer.c:123:10: error: incompatible types when assigning to type ‘int[5]’ from type ‘int *
proposer.c:125:10: error: incompatible types when assigning to type ‘int[5]’ from type ‘int (*)[5]’
第二种方法:因此,我尝试了另一种方法,将接收到的数据包转换为元数据指针。然后我将
received_-struct
指定为指向received_-struct
的指针。此时,我的figurereceived_struct
现在是“正常”格式,既不是指针也不是地址。然而,这也不起作用
if(uip_newdata()) {
struct metadata* received_struct_ptr;
received_struct_ptr = (struct metadata*) uip_appdata;
struct metadata received_struct;
received_struct = *received_struct_ptr;
int data[5];
data = received_struct.values;
}
错误:
static void trickle_tx(void *ptr, uint8_t suppress)
{
uip_ipaddr_copy(&trickle_conn->ripaddr, &ipaddr);
uip_udp_packet_send(trickle_conn, &packet, sizeof(packet));
/* Restore to 'accept incoming from any IP' */
uip_create_unspecified(&trickle_conn->ripaddr);
leds_off(LEDS_RED);
}
提议人.c:125:10:错误:分配给类型时类型不兼容
“int*”类型中的“int[5]”
方法3:对于这种方法,我将传入数据强制转换为
metdata
指针,然后检索该指针指向的给定结构,并将其分配给data\u struct
,然后尝试访问数据
if(uip_newdata()) {
struct metadata* struct_pointer;
struct_pointer = (struct metadata*) uip_appdata;
struct metadata data_struct;
data_struct = *struct_pointer;
int data [5];
data = &data_struct.values;
错误:
if(uip_newdata()) {
struct metadata received_struct;
received_struct = (struct metadata) *uip_appdata;
int data[5];
data = received_struct.values;
}
proposer.c:120:31: error: invalid use of void expression
proposer.c:123:10: error: incompatible types when assigning to type ‘int[5]’ from type ‘int *
proposer.c:125:10: error: incompatible types when assigning to type ‘int[5]’ from type ‘int (*)[5]’
老实说,我真的不知道这里发生了什么。我原以为它会像获取指向内存地址的指针、获取结构并访问它一样简单,但事实并非如此。我想是的,但我似乎不完全理解指针
任何帮助都将不胜感激。您的第二种方法是正确的
received_struct_ptr = (struct metadata*) uip_appdata;
struct metadata received_struct;
received_struct = *received_struct_ptr;
int data[5];
您已将指针强制转换为正确的类型,并将接收到的数据分配给结构。但C语言中没有内置功能将数组的每个成员分配给另一个数组,因此需要手动执行:
int i;
for(i=0; i<5; ++i)
{
data[i] = received_struct.values[i];
}
inti;
对于(i=0;iA指针不是数组,数组也不是指针。数组可以衰减为指向其第一个元素的指针(即someArray
衰减为&someArray[0]
)。您不能分配给数组,但可以分配给它。