accessing double pointer to structure

2019-06-13 15:28发布

问题:

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);
}

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.

回答1:

-> 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.

I think you want:

*(*control->eth_frame)->crc_32

or possibly just

*control->eth_frame[0]->crc_32

In other words:

control is an eth_crtl_t*

control->eth_frame is a frame_t**

*control->eth_frame is a frame_t*

(*control->eth_frame)->crc_32 is a unsigned int*

*(*control->eth_frame)->crc_32 is an unsigned int

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.



标签: c structure