c - 我正在编写一个代码,在其中创建人、与他们结婚、让他们生 child 等,但我无法打印它们

标签 c

所以一开始,我不知道我必须使用 struct,所以我惨败了,不得不更改我的整个代码,但现在它无法打印任何内容:(

#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 mother[20] 类型的代码而不是 struct mother 编写了它们,我很纠结。我无法打印出人类信息,它应该如下所示:

================ 
Name   : Steven
Sex    : male 
Year   : 1970 
Father : NA 
Mother : NA 
Sig.O  : Eva
Child 1: Laura
Child 2: Alice 
================ 

上面的代码只是我在主代码中使用的没有名称的表示,只是为了让您明白它的含义。提前致谢!

最佳答案

你的代码可以在你的机器上编译吗,我试过了,但它不能在我的机器上编译。 您需要先修复代码才能编译它,例如更改此

Person *p = calloc(1,sizeof(*p));

Person *p = (Person *)calloc(1, sizeof(Person));

并将 strcmp(p->father, "") 更改为 strcmp(p->father->name, "")

p->father 不是字符串,而是结构体。您无法将结构与字符串进行比较。

对母亲和 child 也做同样的事情。

关于c - 我正在编写一个代码,在其中创建人、与他们结婚、让他们生 child 等,但我无法打印它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58904436/

相关文章:

c - 转换为 char 的浮点值

c - 指针的数组表示法

c - 读取结构大小时出现问题

c - Windows 需要同时执行内联和非内联函数。为什么?

c - ANSI C,没有可变参数函数的整数到字符串

c - 段错误 :11, 代码在 Windows 上有效,但在 Mac 上无效

c - 传递 const 变量时不兼容的指针类型

c - 如何从 C 调用 MATLAB 代码?

c - 如何在C中给定范围内随机排列数组中的随机数

java - 将 jobject 与值相关联