0
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?

Anik Shahriar
  • 151
  • 1
  • 11
  • 3
    If `name` is a `char[25]` array, what do you suppose `name[0]` is ? `strcpy` requires a `const char*` for the second parameter; `name[0]` is *not* that. – WhozCraig Nov 14 '16 at 16:47
  • 1
    Note that the error message says "integer" not `int`. The `char` type (that is each array element) is an integer type. – Weather Vane Nov 14 '16 at 16:50
  • 1
    What do you want to compare? Only first character of name and c? What about `if (name[0] == c[0]) {...`? Or all characters from c to the beginning of name `if (strncmp(c, name, strlen(c)) == 0) {...` – Serge Ballesta Nov 14 '16 at 16:50
  • Aside: `while (!feof(fp)){` ==> `while (fscanf (fp, "%s %d", name, &n) == 2){` and remove the two other `fscanf` statements. The loop is best controlled by testing for correct conversion. – Weather Vane Nov 14 '16 at 16:53
  • it is also a good habit to compile with full flags like -Wall -Wextra -Werror, as you will see exactly whats wrong – LotoLo Nov 14 '16 at 17:05

2 Answers2

1

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Ari0nhh
  • 5,720
  • 3
  • 28
  • 33
  • I want to get the first character from 'name' and compare it with c. How should i do it? – Anik Shahriar Nov 14 '16 at 16:51
  • @AnikShahriar: since `c` is an array too, presumably you need `if (c[0] == name[0])` to compare the two first characters. – Jonathan Leffler Nov 14 '16 at 16:52
  • `c` is an array as well. How you could compare set of characters with one character? – Ari0nhh Nov 14 '16 at 16:52
  • With `strchr` function. – Weather Vane Nov 14 '16 at 16:54
  • 1
    @AnikShahriar : If that is what you want to do, then you should ask that question. `char`s are integer types and can be compared directly with the `==` operator: `if( c[0] == name[0] )`. Possibly `c` need not be an array but just a single `char`, (i.e. `char c ;`) in which case the `scanf` should change to `scanf ("%c",&c);` – Clifford Nov 14 '16 at 17:00
1

There are a number of problems.

The below code fixes the above issues:

void viewonechar(void) 
{
    char name[25], c[25];
    int n;

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

    if (!fp) {
        perror("fopen");
        exit(1);
    }

    printf ("\n\n Enter Character : ");
    if (fgets(c, sizeof c, stdin) == NULL) {
        fprintf(stderr, "Input error\n");
        exit(1);
    }

    while (fscanf (fp, "%24s %d", name, &n) == 2) {
        if (c[0] == name[0]) {
            printf (" %s +880%d\n",name, n);
        }  
    }
    printf ("\n\n");
    fclose(fp);
    menu();
}
Community
  • 1
  • 1
P.P
  • 117,907
  • 20
  • 175
  • 238