C 在具有联合的typedef结构中,是否需要实例化所有变量?

C 在具有联合的typedef结构中,是否需要实例化所有变量?,c,C,我正在学习C,如果这个问题问得不对,我道歉 但我的问题是,如果我有一个typedef结构,它包含另外两个typedef结构的并集,我需要实例化所有变量吗 例如,如果我有以下代码: typedef struct dog { char *breed; long weight; } dog; typedef struct cat { bool isBlackCat; int numberOfLives; } cat; typedef struct ani

我正在学习C,如果这个问题问得不对,我道歉

但我的问题是,如果我有一个typedef结构,它包含另外两个typedef结构的并集,我需要实例化所有变量吗

例如,如果我有以下代码:

typedef struct dog {

    char *breed;
    long weight;

} dog;

typedef struct cat {

    bool isBlackCat;
    int numberOfLives;

} cat;



typedef struct animal {

    int id;
    int ownerID;

    char *name;
    bool isDog;

    union {
        struct dog aDog;
        struct cat aCat;
    } animals;

} animal;
如果我在这样的函数中使用上述结构:

Adoption adoptAnimal(char *nameParam, bool type, int ownerId) {

    Adoption pet = (Adoption) malloc(sizeof(struct animal));

    pet -> name = (char*) malloc((1 + strlen(nameParam)) * sizeof(char));
    strcpy(pet->name, nameParam);


    pet -> id = getId(); //function that generates an id

    pet -> ownerId = ownerId;
    pet -> isDog = type;



    // if type = 1, it's a dog
    if (type) {
        pet -> animals.aDog.breed = some breed; 
        pet -> animals.aDog.weight = some weight; 


    } else {
        pet -> animals.aCat.lives = 9; 
        pet -> animals.aCat.isBlackCat = 1; 

    }

    return pet;
}
这是否正确/合法地使用了包含联合的typedef结构? 函数的设置方式是,将分配dog或cat结构中的变量,但不能同时分配这两个变量,这是允许的吗?或者我需要分配两个结构中的所有变量吗


谢谢

这很棘手,因为C的类型非常松散。
animal
结构中的
animals
联合将被视为狗或猫,具体取决于您访问它的方式

请记住,联合是单个内存位置,它是最大结构成员的大小。这意味着调用结构成员
animals
会产生误导,因为它只有一个动物(猫或狗,但决不能两者都有)

因此,当您执行
pet->anives.aCat.lifes=9
时,
anies
联合被视为
cat
结构,并且该结构的lifes成员被设置为9。然而,你可以尝试阅读<代码>动物< /代码>联合作为狗,C会认为这是有效的。你只会得到毫无意义的数据

我对您的代码做了一些修改,这样它就可以编译并创建一个主函数来提供一些关于正在发生的事情的见解。
adoptAnimal()
函数识别被收养的动物是dog类型的,因此
pet->animals
被视为dog结构,并相应地设置成员。然而,回到主函数中,我可以继续阅读动物联盟作为狗或猫,C不在乎

typedef struct dog {
    char *breed;
    long weight;
} dog;

typedef struct cat {
    bool isBlackCat;
    int numberOfLives;
} cat;

typedef struct animal {
    int id;
    int ownerID;

    char *name;
    bool isDog;

    union {
        struct dog aDog;
        struct cat aCat;
    } animals;
} animal;

animal* adoptAnimal(char *nameParam, bool type, int ownerId) {
    animal *pet = (animal*) malloc(sizeof(struct animal));

    pet->name = (char*) malloc((1 + strlen(nameParam)) * sizeof(char));
    strcpy(pet->name, nameParam);

    pet->id = 1234; //function that generates an id

    pet->ownerID = ownerId;
    pet->isDog = type;

    // if type = 1, it's a dog
    if (type) {
        pet->animals.aDog.breed = malloc(10); 
        strcpy(pet->animals.aDog.breed, "Pit bull");
        pet->animals.aDog.weight = 1000; 

    } else {
        pet->animals.aCat.numberOfLives = 9; 
        pet->animals.aCat.isBlackCat = 1; 

    }

    return pet;
}

int main() {
    animal *pet = adoptAnimal("George", 1, 1234);

    printf("Dog: breed=\"%s\", weight=\"%ld\"\n", pet->animals.aDog.breed, pet->animals.aDog.weight);
    printf("Cat: lives=\"%d\", isBlack=\"%d\"\n", pet->animals.aCat.numberOfLives, pet->animals.aCat.isBlackCat);

    /* Output:
     * Dog: breed="Pit bull", weight="1000"
     * Cat: lives="32735", isBlack="0"
     */
}
因此,总之,对结构成员使用union是有效的,您只需要为dog结构或cat结构设置值,但不能同时设置两者。还要确保使用适当的命名和相关变量来理解工会持有的数据

我喜欢,如果你还没看过的话,你可能想看看


希望这能澄清问题,如果我错过了什么,请告诉我

将分配dog或cat结构,但不能同时分配两者,这是允许的吗?当然,这是
联合的主要目的,共享内存中的区域,但每次使用一个。你的代码在我看来很好,我在
struct aDog
中没有看到
size
成员,但可能你没有显示完整的代码,或者你的意思是
weight
而不是
size
@DavidRanieri真棒,谢谢!谢谢你的接球,这是为了增加重量而不是尺寸。非常感谢!还有两件事:首先,为什么在动物结构中使用
struct dog/cat
?您使用了
typedef
,因此可以只编写
dog/cat
。否则您就不需要
typedef
。第二,不要强制转换
void*
:不需要强制转换。建议
pet->name=malloc(strlen(nameParam)+1)