【发布时间】:2019-11-17 19:37:56
【问题描述】:
所以一开始,我不知道我必须使用结构,所以我失败了,不得不更改我的整个代码,但现在它无法打印任何东西:(
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct p {
char* name;
int year_of_birth;
char* sex;
struct p* mother;
struct p* father;
struct p* significant_other;
struct p** children;
}typedef Person;
这是我定义我的结构的地方
Person* person_constructor(char *name, int year_of_birth, char *sex);
Person* birth(char *name, int year_of_birth, char *sex, Person *mother);
void display_person(Person* p);
void marry_them(Person *p1, Person *p2);
void display_family(Person* family[], int n);
Person* sibling(Person p, int print);
这是我的函数定义
int main() {
Person* p1 = person_constructor("Abbas", 1970, "male");
Person* p2 = person_constructor("Sidika", 1970, "female");
marry_them(p1, p2);
Person* p3 = person_constructor("Pinar", 1990, "female");
Person* p4 = birth("Siamak", 1990, "male", p2);
marry_them(p3, p4);
Person* p5 = birth("Guzide", 1990, "female", p2);
Person* p6 = person_constructor("Fatih", 1990, "male");
marry_them(p5, p6);
Person* p7 = birth("Berkecan", 2010, "male", p3);
Person* p8 = birth("Ekinsu", 2010, "female", p3);
Person* p9 = birth("Canim", 2010, "female", p5);
display_person(p1);
我召唤人们、与他们结婚等最重要的部分,
return 0;
}
Person* person_constructor(char *name, int year_of_birth, char *sex)
{
Person *p = calloc(1,sizeof(*p));
strcpy(p->name, name);
p->year_of_birth = year_of_birth;
strcpy(p->sex, sex);
return p;
}
void display_person(Person* p)
{
printf("================\n");
printf("Name : %s \n",p->name);
printf("Sex : %s \n",p->sex);
printf("Year : %d \n",p->year_of_birth);
if (strcmp(p->father,"") == 0){
printf("Father : NA \n");
}
else {
printf("Father : %s \n", p->father);
}
if (strcmp(p->mother,"") == 0){
printf("Mother : NA \n");
}
else {
printf("Mother : %s \n", p->mother);
}
if (strcmp((const char *) p->significant_other, "") == 0){
printf("Sig.O : NA \n");
}
else {
printf("Sig.O : %s \n", p->significant_other);
}
if (strcmp(p->children,"") == 0){
printf("Child 1 : NA \n");
}
else {
printf("Child 1 : %s \n", p->children[0]);
}
if (strcmp(p->children,"") == 0){
printf("Child 2 : NA \n");
}
else {
printf("Child 2 : %s \n", p->children[1]);
}
}
void marry_them(Person *p1, Person *p2)
{
strcpy(p1->significant_other, p2->name);
strcpy(p2->significant_other, p1->name);
}
Person* birth(char *name, int year_of_birth, char *sex, Person *mother) {
Person *p = person_constructor(name, year_of_birth, sex);
strcpy(p->mother, mother->name);
strcpy(p->father, mother->significant_other);
return p;
}
void display_family(Person* family[], int n)
{
for (int i = 0;i<n;i++)
{
display_person(family[i]);
}
}
Person* sibling(Person p, int print)
{
}
这些是我的函数,我为 char mom[20] 类型的代码而不是 struct mom 编写了它们,我很纠结。我无法打印出人类信息,它应该是这样的:
================
Name : Steven
Sex : male
Year : 1970
Father : NA
Mother : NA
Sig.O : Eva
Child 1: Laura
Child 2: Alice
================
上面的代码只是我在主要代码中使用的没有名称的表示,只是为了让你明白它的意思。提前致谢!
【问题讨论】:
-
查看
strcpy(p->name, name);。当它被调用时,p->name是NULL。 -
@chux-ReinstateMonica 为什么是NULL,我第一次召唤person_constructor时不应该取阿巴斯的名字吗?
-
@chux-ReinstateMonica 我把它变成了 p->name = name;看起来是可行的,但仍然有很多缺陷
-
p = calloc(1,sizeof(*p));将p->name设置为0。发生在strcpy()之前。p->name需要一些分配的内存,如果你想要独特的名字。试试p->name = strdup(name); -
“孩子”有问题。我找不到它们设置的任何地方,但代码仍然打印它们。声明为 struct p**,但用作字符串。考虑删除所有像 'const char *)` 这样的强制转换,让编译器告诉你类型不匹配
标签: c