指针作为形参传递值
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct gvr_pdu
{
int uiLength;
char *pucContent;
}Gvr_pdu;
typedef enum {
UID_CNTRL = 0,
UID_BPDU
} UID_CMD_TYPE_T;
typedef enum {
UID_PORT_CONNECT,
UID_PORT_DISCONNECT,
UID_BRIDGE_SHUTDOWN,
UID_BRIDGE_HANDSHAKE,
UID_LAST_DUMMY
} UID_CNTRL_CMD_T;
typedef struct uid_port_control_s {
UID_CNTRL_CMD_T cmd;
unsigned long param1;
unsigned long param2;
} UID_CNTRL_BODY_T;
typedef struct uid_msg_header_s {
UID_CMD_TYPE_T cmd_type;
long sender_pid;
int destination_port;
int source_port;
int body_len;
} UID_MSG_HEADER_T;
typedef struct uid_msg_s {
UID_MSG_HEADER_T header;
union {
UID_CNTRL_BODY_T cntrl;
char *bpdu ;
} body;
} UID_MSG_T;
void test(UID_MSG_T *msg)
{
int port_no;
int i;
Gvr_pdu sGvrpPud ={0} ;
for(i=0;i<sGvrpPud.uiLength;i++){
printf("puc[%d]:%d\n",i,msg->body.bpdu[i]);
}
port_no = msg->header.source_port;
sGvrpPud.uiLength = msg->header.body_len ;
printf("port num:%d \t uiLength:%d\n",port_no,sGvrpPud.uiLength);
memcpy(&sGvrpPud.pucContent,msg->body.bpdu,sGvrpPud.uiLength);
for(i=0;i<sGvrpPud.uiLength;i++){
printf("puc[%d]:%d\n",i,sGvrpPud.pucContent[i]);
}
}
int main()
{
char buff[sizeof(UID_MSG_T)];
UID_MSG_T *msg =(UID_MSG_T *)malloc(sizeof(UID_MSG_T));
msg->body.bpdu =(char *)malloc(sizeof(UID_MSG_T));
UID_MSG_T *temp;
char str[100]={0};
int i,n=0;
for(i=0;i<sizeof(UID_MSG_T);i++){
buff[i]=i;
}
printf("buff length:%d\n",strlen(buff));
msg->header.sender_pid = 100;
msg->header.cmd_type =UID_BPDU;
msg->header.source_port=1;
msg->header.body_len=sizeof(UID_MSG_T);
memcpy(msg->body.bpdu,buff,sizeof(UID_MSG_T));
for(i=0;i<sizeof(UID_MSG_T);i++){
printf("%d\n",msg->body.bpdu[i]);
}
test(msg);
free(msg->body.bpdu);
free(msg);
return 0;
}
在函数test中把结构体msg作为形参,在函数内输出msg->body.bpud的值为什么会不存在?
怎么才能实现msg->body.bpdu的值赋给sGvrpPud.pucContent 指针 C
[解决办法]
sGvrpPud.uiLength 没值?