user3041923
user3041923

Reputation: 19

String comparision in c

I am trying to test the name.The users need to type name again when the name had already existed.But the program clashes when run.Thanks for your help!

{
#define MAX 3
char *Mystr2[40];
char Mystr1[40];
int i,k,j;

for(i=0;i<MAX;i++)
{
    printf("Enter the name:");
    gets(Mystr1);

    Mystr2[j]=Mystr1;//i want to save the string into Mystr[0].[1]

    for (j=0;j<i;j++)//Test the name whether it is same or not
    {
        if(strcmp(Mystr2[j],Mystr2[i])==0)
        {
            printf("They are the same");
                            i--;
                            break;
        }
    }
}
return 0;

}

Upvotes: 0

Views: 111

Answers (1)

Edward Clements
Edward Clements

Reputation: 5152

Your crash is because Mystr2[j]=Mystr1, I thnk you meant Mystr2[i]=Mystr1.

The logic will not work anyway, since Mystr2 will always point to what is in Mystr1 at that moment; change the declaration to char Mystr2[MAX][40] and strcpy() into it.

Upvotes: 3

Related Questions