【问题标题】:I am making a code where I create persons, marry them, make them give birth etc but I fail at printing them我正在编写代码,在其中创建人,与他们结婚,让他们生育等,但我无法打印它们
【发布时间】: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-&gt;name, name);。当它被调用时,p-&gt;nameNULL
  • @chux-ReinstateMonica 为什么是NULL,我第一次召唤person_constructor时不应该取阿巴斯的名字吗?
  • @chux-ReinstateMonica 我把它变成了 p->name = name;看起来是可行的,但仍然有很多缺陷
  • p = calloc(1,sizeof(*p));p-&gt;name 设置为0。发生在strcpy() 之前。 p-&gt;name 需要一些分配的内存,如果你想要独特的名字。试试p-&gt;name = strdup(name);
  • “孩子”有问题。我找不到它们设置的任何地方,但代码仍然打印它们。声明为 struct p**,但用作字符串。考虑删除所有像 'const char *)` 这样的强制转换,让编译器告诉你类型不匹配

标签: c


【解决方案1】:

您的代码是否可以在您的机器上编译,我试过了,但它没有在我的机器上编译。 你需要先修复代码才能编译它,比如改变这个

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

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

并将strcmp(p-&gt;father, "") 更改为strcmp(p-&gt;father-&gt;name, "")

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

同样为母亲和孩子做。

【讨论】:

  • 我可以建议进行更多更改,以便您的代码编译良好。尝试一下,如果需要进一步帮助,请告诉我。
  • 我删除了所有 strcpy 并将它们替换为 p->year_of_birth = year_of_birth;还是不行啊
  • calloc 语句是相同的,但是带有多余转换的第二个语句更难维护和阅读,(尽管 C++ 需要它。)p-&gt;father 不是一个有效的指针,那么p-&gt;father-&gt;name 要崩溃了。
【解决方案2】:

person_constructor 函数中可能存在问题。尝试这样做:

Person* person_constructor(char *name, int year_of_birth, char *sex)
{
    Person *p = (Person *)calloc(1, sizeof(Person));
    strcpy(p->name, name);
    p->year_of_birth = year_of_birth;
    strcpy(p->sex, sex);
    return p;
}

对于 display_person 函数,父函数不是字符串,而是指针,因此它们是 null 或结构体:

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);

    printf("Father : %s \n", p->father == null ? "N/A" : p->father->name);
    printf("Mother : %s \n", p->mother== null ? "N/A" : p->mother->name);

    printf("Sig.O  : %s \n", p->significant_other== null ? "N/A" : p->significant_other->name);
    printf("Child 1: %s \n", p->children[0]== null ? "N/A" : p->children[0]->name);

    //Others here
}

【讨论】:

    【解决方案3】:

    就目前而言,已经定义:

    然后使用calloc 获取此实例:

    这很好,但请注意指针无效。 strcpy 将在无效指针上是未定义的行为。可以p-&gt;name = name,这将导致编译器将name 指向name 参数:

    由于使用static constant 名称,该名称将永远不会消失,也不会更改。只要控制name 的来源,这就很好。假设name 在被传递给person_constructor 之前被输入到一个临时的char *;只要存在基础临时变量,一个人只会有一个定义的name。避免这种情况的常用方法是复制。

    Person *p;
    char *pname;
    size_t name_len = strlen(name);
    p = calloc(1, sizeof *p + name_len + 1);
    if(!p) return 0;
    pname = (char *)(p + 1);
    strcpy(name, pname);
    p->name = pname;
    

    这会将name 参数复制到p 所在空间之后的新形成的p 中(因为分配了该空间。)另一种选择是分配固定的最大字符数name[200]并将名称直接复制进去,(注意strcpy不会造成溢出。)

    strcmp(p-&gt;father,""),当它期望const char * 时传递struct p(请参阅documentation。)strcmp 函数对struct p 一无所知,但会愉快地返回无关值或崩溃。我建议在构造函数中将father 显式设置为0 (NULL),(不保证全字节为零,)然后可以printf("Father : %s \n", p-&gt;father ? p-&gt;father-&gt;name : "N/A");

    对于孩子也是一样,除了birth 会导致数组增长。如果它是一个动态数组,则需要realloc 一些东西。另一种方法是分配固定的最大子节点数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-06
      • 1970-01-01
      • 2012-03-26
      • 2013-01-04
      • 2021-05-01
      • 2010-10-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多