C语言结构类型转换,发现自己没办法理解了~~~恳请大神拍醒~~~

2019-07-21 00:48发布

[mw_shl_code=c,true]#endif /* SO_REUSE */ lpcb = (struct tcp_pcb_listen *)memp_malloc(MEMP_TCP_PCB_LISTEN); /*lwip??????????tcp_pcb_listen?á??????ר???è??LISTEN×????????á????*/ if (lpcb == NULL) { return NULL; } lpcb->callback_arg = pcb->callback_arg; /*??tcp_pcb????????±???tcp_pcb_listen????*/ lpcb->local_port = pcb->local_port; lpcb->state = LISTEN; /*?è?????ì??×???*/ lpcb->prio = pcb->prio; lpcb->so_options = pcb->so_options; ip_set_option(lpcb, SOF_ACCEPTCONN); lpcb->ttl = pcb->ttl; lpcb->tos = pcb->tos; ip_addr_copy(lpcb->local_ip, pcb->local_ip); if (pcb->local_port != 0) { TCP_RMV(&tcp_bound_pcbs, pcb); /*tcp_bound_pcbs(??°ó?¨?ê±?????±í),??tcp_bound_pcbs??±í???????????ì*/ } memp_free(MEMP_TCP_PCB, pcb); /*??·?????????*/ #if LWIP_CALLBACK_API lpcb->accept = tcp_accept_null; /*???????§???????????????÷????*/ #endif /* LWIP_CALLBACK_API */ #if TCP_LISTEN_BACKLOG lpcb->accepts_pending = 0; lpcb->backlog = (backlog ? backlog : 1); #endif /* TCP_LISTEN_BACKLOG */ TCP_REG(&tcp_listen_pcbs.pcbs, (struct tcp_pcb *)lpcb);/*如何理解这里的结构类型转换???*/ return (struct tcp_pcb *)lpcb; }[/mw_shl_code] 按照小弟的理解:这里的lpcb指针已经被初始化了,它的类型是struct tcp_pcb_listen *,如何将它强制转换为struct tcp_pcb *,毕竟2者所指向的变量所占用的内存空间不同~
恳请大神拍醒我.......
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
4条回答
ianhom
1楼-- · 2019-07-21 05:52
或者说:如何理解结构体之间的强制类型转换~
例如:struct a
{
char a;
};

struct b
{
int a;
char b;
}
它们之间如何进行指针强制转换呢?

struct b B = {0x12345678,0x9A};
struct a * pA = (struct a*)(&B);
这个时候就可以用结构体a的格式来访问(看待)结构体b的实体数据, (例如小端模式下,pA->a的值为0x78)

强制类型转换只是变换了对数据的看法,原始的数据不会变化,这样能给我解析数据很多方便。
比如
struct x
{
    int X;
};

struct Y
{
    short int Y1;
    short int Y2;
};

struct Z
{
    char Z1;
    char Z2;
    char Z3;
    char Z4;
};

struct X Test = {0x12345678};
如果我想检查一个int型变量的最高位字节,
那((struct Z*)Test)->Z4就能得到。
((struct Z*)Test)->Z1就能得到低位

其实用union更方便。
liuchang
2楼-- · 2019-07-21 06:38
或者说:如何理解结构体之间的强制类型转换~
例如:struct a
{
char a;
};

struct b
{
int a;
char b;
}
它们之间如何进行指针强制转换呢?恳请大神指教~
正点原子
3楼-- · 2019-07-21 11:06
 精彩回答 2  元偷偷看……
liuchang
4楼-- · 2019-07-21 14:31
回复【4楼】ianhom:
---------------------------------
感谢大神赐教

一周热门 更多>