访问结构的双指针 [英] accessing double pointer to structure
问题描述
我正在尝试使用以下结构指针构建数据包
I am trying to frame a data packet using the following structure pointer
typedef struct address {
unsigned char *mac_destn_addr;
unsigned char *mac_src_addrl
}address_t;
typedef struct frame {
address_t *eth_addr;
unsigned char *payload;
unsigned int *crc32;
}frame_t;
typedef struct eth_ctrl {
unsigned char *no_of_pkt;
unsigned short *delay;
frame_t **eth_frame;
}eth_ctrl_t;
address_t *adr;
frame_t *frame;
eth_ctrl_t *control;
void main(int argc, char *argv[])
{
adr = malloc(sizeof(address_t));
frame = malloc(sizeof(frame_t));
control = malloc(sizeof(eth_ctrl_t));
frame->eth_addr = adr;
control->eth_frame = &frame;
printf("CRC32 : 0x%x\n", (*control)->eth_frame->crc32);
}
它打印crc32
变量的地址.我需要打印出现在该地址的值.我尝试使用 *(*control)->eth_frame->crc32
, **(control->eth_frame->crc32)
它不打印正确的值.
it prints the address of crc32
variable. I need to print the value which is present at that address.
i tried with *(*control)->eth_frame->crc32
, **(control->eth_frame->crc32)
it doesn't prints the correct value.
推荐答案
->
希望 LHS 是一个指针,所以任何以 (*control)->
肯定会做一些奇怪的事情,因为 control
只是一个指针.
->
expects the LHS to be a pointer, so anything starting with (*control)->
is guaranteed to do something weird since control
is only a single pointer.
我想你想要:
*(*control->eth_frame)->crc_32
或者可能只是
*control->eth_frame[0]->crc_32
换句话说:
control
是一个 eth_crtl_t*
control->eth_frame
是一个 frame_t**
*control->eth_frame
是一个 frame_t*
(*control->eth_frame)->crc_32
是一个 unsigned int*
*(*control->eth_frame)->crc_32
是一个 unsigned int
control
是一个指针.取消引用它,并获取名为 eth_frame
的元素.那是一个双指针.取消引用,再次取消引用,然后获取名为 crc_32
的元素.那是单个指针.仅取消引用一次.
control
is a pointer. Dereference it, and take the element called eth_frame
. That is a double pointer. Dereference it, dereference it again, and take the element called crc_32
. That is a single pointer. Dereference it only once.
这篇关于访问结构的双指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!