嵌套结构和动态内存分配的问题

Issues with nested structures and dynamic memory allocation

我正在使用嵌套结构和动态内存分配,但遇到了问题。

我正在使用以下输入:

会员编号:1
会员姓名:A
保单编号:1

政策编号:P1
保单类型:1
保费:100

但是,我进入P1后无法输入任何其他内容。程序终止。

我遇到的另一个问题是,当我 select 应该打印所有成员的选项 2 时,它没有正确显示。

即使在输入任何会员详细信息之前,如果我选择选项 2,它也会显示以下内容:

会员编号:0
会员姓名:

我在想是不是我在创建动态数组的时候搞错了,居然打印的是member[0]。 将成员添加到数组后,然后选择选项 2,它会打印:

会员编号:1
会员姓名:A
策略 ID:(空)
保单类型:保费:$100

它会打印成员详细信息,但是在访问该成员的策略时,不会打印这些详细信息,并且它会完全跳过 政策类型。

完整代码如下:

#include <malloc.h>
#include <stdio.h>

enum enumtype { Car, Health, Travel, Pet };

typedef struct policy {
        char id[4];
        enum enumtype type;
        int premium;
}pol;

typedef struct member {
    int id;
    char name[30];
    int polnum;
    pol *policy;
}mem;

mem getmember() {
    mem member;
    int i, j;

    printf("\n\nEnter member's details");
    printf("\nMember ID: ");
    scanf("%d", &member.id);
    printf("Member Name: ");
    scanf(" %s", member.name);
    printf("Requested number of policies: ");
    scanf("%d", &member.polnum);

    if (member.polnum > 0) { 
        printf("\nEnter policy details for %s", member.name);
        for (i = 0; i < member.polnum; i++) {
            printf("\nEnter details for policy %d", i + 1);
            printf("\nEnter Policy ID: ");
            scanf(" %s", member.policy->id);

            printf("\nEnter Policy Type:");
            printf("\n1 - Car");
            printf("\n2 - Health");
            printf("\n3 - Travel");
            printf("\n4 - Pet");
            printf("\n\nEnter an option: ");
            scanf("%d", &member.policy[i].type);

            while (member.policy[i].type < 1 || member.policy[i].type > 4) {
                printf("\nInvalid policy type");
                printf("\nPlease enter a number from 1 - 4: ");
                scanf("%d", &member.policy[i].type);
            }

            printf("\nEnter Premium: $");
            scanf("%d", &member.policy[i].premium);

            while (member.policy[i].premium < 0) {
                printf("Invalid premium");
                printf("\nPremium must be a positive value");
                printf("\nEnter Premium: $");
                scanf("%d", &member.policy[i].premium);
            }
        }
    }
    else
        member.policy = NULL;
    
    return member;
}

void printmember(mem* member, int memNum) {

    int i, j;

    for (i = 1; i = memNum+1; i++) { // This still doesn't print properly but I don't know why
        //printf("\nDetails for Member %d", i + 1);
        printf("\n\nMember ID: %d", member[i].id);
        printf("\nMember Name: %s", member[i].name);

        for (j = 0; j < member[i].polnum; j++) {
            printf("\nPolicy ID: %s", member[i].policy[j].id);
            printf("\nPolicy Type: ", member[i].policy[j].type);
            switch (member[i].policy[j].type) { 
            case 1:
                printf("Car");
                break;
            case 2:
                printf("Health");
                break;
            case 3:
                printf("Travel");
                break;
            case 4:
                printf("Pet");
                break;
            }
            
            printf("Premium: $%d", member[i].policy[j].premium);
        }
    }
}

int main()
{
    int choice = 0;
    int memNum = 0;
    char name[30];
    mem* member = NULL;
    member = (mem*)realloc(member, (memNum++) * sizeof(member));


    while (choice != 3) {
        
        choice = 0;
        printf("\n\t   Menu");
        printf("\n-----------------------------");
        printf("\n1 - Add new member");
        printf("\n2 - Display all members");
        printf("\n3 - Exit");
        
        printf("\n\nEnter an option: ");
        scanf("%d", &choice);
        
        switch (choice) {
        case 1:
            member = (mem*)realloc(member, (memNum + 1) * sizeof(member));
            member[memNum++] = getmember();
            break;
        case 2:
            printf("\n\nDisplaying all members");
            printmember(member, memNum);
            break;
        case 3:
            return 0;
        }
    }
}
typedef struct member {
    int id;
    char name[30];
    int polnum;

    struct policy {
        char id[4];
        enum enumtype type;
        int premium;
    }pol;
}mem;

你可以有 n 个成员,每个成员可以有 polnum 个策略,但这不是这个结构的意思。这个结构表示每个成员都有一个由嵌套结构表示的策略。您的外部 realloc 将成员保留在数组中会限制解决方案 space。我们实际上不能同时使用弹性结构来做到这一点,所以我不必深入探讨如何做。我们只是按照您已经知道的方式来做这件事。

typedef struct struct policy {
        char id[4];
        enum enumtype type;
        int premium;
} pol;

typedef struct member {
    int id;
    char name[30];
    int polnum;
    pol *policies;
}mem;

添加策略是这样的:

    if (member.polnum > 0) { 
        member.pol = malloc(member.polnum * sizeof(pol));
        printf("\nEnter policy details for %s", member.name);
        for (i = 0; i < member.polnum; i++) {
             //...
             scanf("%d", &member.pol[i].type);
             //...
         }
     } else {
         member.pol = NULL; // When you go to write the free memory code you will thank me
     }

在 print 成员中,我们有一个非常相似的结构:

        for (j = 0; j < member[i].polnum; j++) {
            printf("\nPolicy ID: %s", member[i].pol[j].type);

管理说明:答案发布后,代码已从问题中删除。