简体   繁体   English

为嵌套结构分配值时出现分段错误

[英]Getting segmentation fault while assigning values to a nested structure

struct Address
{
        char *streetName;
        char *city;
};

struct Employee{
        char name[32];
        int empId;
        struct Address *add[2];
        struct Employee *next;
};

void fillAddress()
{
        int i;
        struct Employee* node = head;

        while(node != NULL)
        {
                i = 0;
                while(node->add[i++] != NULL);
                strcpy(node->add[i-1]->streetName,strt[i]);< This is where the SEGFAULT OCCURS>
                strcpy(node->add[i-1]->city,cty[i]);
                node=node->next;
        }
}

While assigning the value to the node->add[i-1]->streetName, it is crashing.在将值分配给 node->add[i-1]->streetName 时,它​​崩溃了。 Have tried to allocate memory to the inner structure but still it crashes.试图为内部结构分配内存,但它仍然崩溃。

Any help here can be appreciated.此处的任何帮助都将不胜感激。

Thanks Santosh谢谢桑托什

It is because struct Address *add[2] is an array of pointers to Address type structure.这是因为 struct Address *add[2] 是一个指向 Address 类型结构的指针数组。 Those pointers are invalid because you didn't allocated them.这些指针是无效的,因为您没有分配它们。 It should be something like this它应该是这样的

node->add[0] = malloc(sizeof(struct Address))
node->add[1] = malloc(sizeof(struct Address))

This will also fail because you try to copy the string, but the string is also not allocated.这也会失败,因为您尝试复制字符串,但字符串也未分配。

node->add[0]->streetName = (char*)malloc(stringSize)
node->add[0]->city= (char*)malloc(stringSize)

After this, the assignation should work.在此之后,分配应该工作。

Edit:编辑:

struct Address
{
    char* streetName;
    char* city;
};
struct Employee {
    char name[32];
    int empId;
    struct Address* add[2];
    struct Employee* next;
};


struct Employee* createEmployee(int id, const char *name, const char *street1, const char *city1, const char *street2, const char*city2)
{
    struct Employee* emp = (struct Employee*)malloc(sizeof(struct Employee));

    emp->empId = id; 
    strcpy(emp->name, name); 

    emp->add[0] = (struct Address*)malloc(sizeof(struct Address));
    emp->add[0]->streetName = (char*)malloc(256);
    emp->add[0]->city = (char*)malloc(256);

    strcpy(emp->add[0]->city, city1);
    strcpy(emp->add[0]->streetName, street1);

    emp->add[1] = (struct Address*)malloc(sizeof(struct Address));
    emp->add[1]->streetName = (char*)malloc(256);
    emp->add[1]->city = (char*)malloc(256);

    strcpy(emp->add[1]->city, city1);
    strcpy(emp->add[1]->streetName, street1);

    return emp;
}

void insertEmployee(struct Employee** head, struct Employee* emp)
{
    if (*head == NULL)
    {
        emp->next = NULL;
        *head = emp;
    }
    else
    {
        emp->next = (*head);
        *head = emp;
    }
}

void printList(struct Employee* head)
{
    while (head != NULL)
    {
        printf("%s\n", head->name);
        printf("%d\n", head->empId);
        printf("%s\n", head->add[0]->city);
        printf("%s\n", head->add[0]->streetName);
        printf("%s\n", head->add[1]->city);
        printf("%s\n", head->add[1]->streetName);
        head = head->next;
    }
}


int main()
{
    struct Employee* head = NULL;
    struct Employee* emp = NULL;

    emp = createEmployee(1, "name1", "street1", "city1", "street2", "city2");
    insertEmployee(&head, emp);
    emp = createEmployee(2, "name2", "street3", "city3", "street4", "city4");
    insertEmployee(&head, emp);

    printList(head);

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM