gpt4 book ai didi

c - 在c中通过函数填充typedef结构

转载 作者:行者123 更新时间:2023-11-30 16:51:03 26 4
gpt4 key购买 nike

我想按函数填充typedef结构。我尝试过:

typedef struct{
char *first_name, *last_name;
int id;
Date birthday;
} Person;

void ReadPerson(Person* person){
person = (Person*)malloc(sizeof(Person));
person->first_name = readString();
person->last_name = readString();
scanf("%d",&(person->id));
ReadDate(&(person->birthday));
}

主要功能:

void main(){
Person *tmp = NULL;
ReadPerson(tmp);
}

使用 Bad Ptr 值调用 ReadPerson tmp 后。

最佳答案

如果在定义变量时在同一代码段中 malloc 变量,也许会更优雅。如果您定义“Person *tmp”以将信息存储在 main() 中,则还要在 main() 中使用 malloc。删除 ReadPerson() 上的 malloc。

关于c - 在c中通过函数填充typedef结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41911528/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com