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?

5
  • 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. Commented Nov 14, 2016 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. Commented Nov 14, 2016 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) {... Commented Nov 14, 2016 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. Commented Nov 14, 2016 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 Commented Nov 14, 2016 at 17:05

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I want to get the first character from 'name' and compare it with c. How should i do it?
@AnikShahriar: since c is an array too, presumably you need if (c[0] == name[0]) to compare the two first characters.
c is an array as well. How you could compare set of characters with one character?
With strchr function.
@AnikShahriar : If that is what you want to do, then you should ask that question. chars 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);
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();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.