我显然不是在这里指责printf,我可能搞砸了我的mem分配和访问权限,但是我不明白我在哪里做错了。程序在main中的第二个printf上崩溃。如果我评论第二个,它也会在第三个崩溃。实际上,每当我在第一个printf之后访问p时,它就会崩溃!
有人可以解释我在做什么吗?
非常感谢。
typedef struct
{
char * firstname;
char * lastname;
int age;
} person;
person * new_person(char * firstname, char * lastname, int age)
{
person p;
int lf = strlen(firstname);
int ll = strlen(lastname);
p.firstname = (char *)malloc(++lf * sizeof(char));
p.lastname = (char *)malloc(++ll * sizeof(char));
strcpy(p.firstname, firstname);
strcpy(p.lastname, lastname);
p.age = age;
return &p;
}
int main()
{
person * p = new_person("firstname", "last", 28);
printf("nom : %s ; prenom : %s ; age : %d\n", …Run Code Online (Sandbox Code Playgroud)