Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 无效应用‘;尺寸’;结构数据的错误和编译错误_C_Segmentation Fault_Sizeof_Calloc - Fatal编程技术网

C 无效应用‘;尺寸’;结构数据的错误和编译错误

C 无效应用‘;尺寸’;结构数据的错误和编译错误,c,segmentation-fault,sizeof,calloc,C,Segmentation Fault,Sizeof,Calloc,我创建了一个要通过套接字发送的数据类型。 我得到一个编译错误和一个分段错误。 我得到的编译错误是错误:“sizeof”对不完整类型“struct udp\u msg\u t”的应用无效,而分段错误发生在我执行memcpy时。我做错了什么 以下是我的一些代码: 这就是我所关注的结构,我定义了: typedef struct udp_msg { unsigned int udp_eid; u_char udp_prefix; unsigned int udp_loc; }

我创建了一个要通过套接字发送的数据类型。 我得到一个编译错误和一个分段错误。 我得到的编译错误是
错误:“sizeof”对不完整类型“struct udp\u msg\u t”的应用无效
,而分段错误发生在我执行
memcpy
时。我做错了什么

以下是我的一些代码:

这就是我所关注的结构,我定义了:

typedef struct udp_msg {
    unsigned int udp_eid;
    u_char   udp_prefix;
    unsigned int udp_loc;
} udp_msg_t;
在方法中,我分配内存和值:

void method(){
    udp_msg_t * udp_msg;

    udp_msg = (struct udp_msg_t * )calloc(1, sizeof(struct udp_msg_t));
    udp_msg->udp_eid = eid.u.prefix4.s_addr;
    udp_msg->udp_prefix = eid.prefixlen;
    udp_msg->udp_loc = loc->rloc.rloc.s_addr;

    send_rloc_udp_to_floodlight(udp_msg);
}
这种方法实际上通过套接字发送数据:

int send_rloc_udp_to_floodlight(udp_msg_t message) {
    struct sockaddr_in si_other;
    int s, i, slen = sizeof(si_other);
    char buffer[9];

    if ((s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) {
        printf("socket");
    }

    memset((char *) &si_other, 0, sizeof(si_other));

    si_other.sin_family = AF_INET;
    si_other.sin_port = htons(8888);

    if (inet_aton("127.0.0.1", &si_other.sin_addr) == 0) {
        fprintf(stderr, "inet_aton() failed\n");
        exit(1);
    }


    memcpy(buffer, (char *) message.udp_eid, sizeof(unsigned int));
    memcpy(&buffer[4], (char *) message.udp_prefix, sizeof(char));
    memcpy(&buffer[5], (char *) message.udp_loc, sizeof(unsigned int));

    //send the message
    if (sendto(s, buffer, strlen(buffer), 0, (struct sockaddr *) &si_other,
            slen) == -1) {
        printf("sendto()");
    }

    close(s);
    return 0;
}
sizeof(结构udp\u msg\t)
不正确-应该是

sizeof(udp_msg_t)

演员也一样:

(struct udp_msg_t * )

在调用
calloc
之前,尽管这应该被删除,因为它是。

我没有正确获取struct字段的指针值。正确的方法是执行memcpy:

memcpy(buffer, (char *) &message.udp_eid, sizeof(unsigned int));

您是否正向声明了结构?typedef是否在您使用它的行中可见?如果typedef位于头中,是否包含该头?typedef在同一文件中定义,因此可见。无关:
If(sendto(s,buffer,strlen(buffer),0,(struct sockaddr*)&si_other,slen)=-1{
您不需要strlen()在这里,缓冲区不是字符串,可能在意外的地方有NUL。yah忘记了slen,很抱歉,一旦代码编译干净(没有错误,没有警告,并确保使用
gcc-Wall…
)然后,如果仍然出现运行时错误,则是时候开始调试了。请参阅:。接受Paul R的建议。OP您应该使用
sizeof(struct-struct-name)
或使用您键入的定义结构的名称,在本例中为
udp\u msg\u t
。typedef声明了一个名为
udp\u msg\u t
的新类型,它等于
struct udp\u msg
。因此编写
struct udp\u msg\u t
会带来麻烦。
memcpy(buffer, (char *) &message.udp_eid, sizeof(unsigned int));