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

1-2: 返すか、0 か 1 を返す方が理にかなってboolintます。

char siblings(struct family member1, struct family member2)

2: "PAUSE" は "pause" にする必要があります

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 に答える