Anik Shahriar Anik Shahriar - 23 days ago 12
C Question

Passing argument 2 of strcmp makes pointer from integer without a cast

void viewonechar(){

char name[25], c[25];
int n;

fp = fopen("Phonebook.txt","r");

printf ("\n\n Enter Character : ");
scanf ("%s",c);

fscanf (fp, "%s %d", name, &n);

while (!feof(fp)){

if ((strcmp(c, name[0])) == 0){ \\ Warning in here

printf (" %s +880%d\n",name, n);

}

fscanf (fp, "%s %d", name, &n);

}

printf ("\n\n");

fclose(fp);

menu();

}


When i compile the code, on the marked line this warning appears, "Passing argument 2 of strcmp makes pointer from integer without a cast". What exactly am i doing wrong?

Answer

int strcmp ( const char * str1, const char * str2 );

Since name is an array of char, name[0] is a char. strcmp takes char pointers as an arguments, so the char you have supplied is implicitly cast to an int-type and then - to the pointer, which produces an undefined behavior and most likely will lead to the segfault.