0

抱歉,我认为我真的把基于结构的几行代码搞砸了……因为我是新手,最近几天努力理解 C。请检查以下代码并指导我哪里错了……谢谢!

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


struct family{

       char name[20];
       int age;
       char father[20];
       char mother[20];

       };

 //Function to compares two strings and returns 1 or 0  

char siblings(struct family member1, struct family member2)
{
     if(strcmp(member1.mother, member2.mother)==0)
         return 1;
     else
         return 0;
 }

int main()
{

//Following structure variables are decleared

    struct family member1;
    struct family member2;

  //structure variables initilized with a string

    member1.mother = "Rosy";
    member2.mother = "Rosy";

//This function compares two strings and returns 1 or 0

    siblings(member1.mother, member2.mother);

//trying to print resulst with respect to return from function 

     printf("%S\n",siblings(member1.mother, member2.mother)?"yes":"No");


    system("PAUSE");
    return 0;
}
4

2 回答 2

1

替换以下内容:

1-1:%S应该换成%d

printf("%S\n",siblings(member1.mother, member2.mother)?"yes":"No");

bool1-2:返回或int返回 0 或 1会更好。

char siblings(struct family member1, struct family member2)

2:“暂停”应该是“暂停”

system("PAUSE");

3:strcpy用于以下。

member1.mother = "Rosy";
member2.mother = "Rosy";
于 2013-03-14T03:12:04.780 回答
0

代替

member1.mother = "Rosy";
member2.mother = "Rosy";

strcpy(member1.mother, "Rosy");
strcpy(member2.mother, "Rosy");

这是因为成员mother不是指针,它是一个数组

编辑

siblings(member1, member2)不应该给兄弟姐妹打电话siblings(member1.mother, member2.mother)

于 2013-03-14T03:13:04.937 回答