Hello! I was making simple programm for word search, but found an error I can't explain.
This code piece of code that causes it is:

                if (word==search){
                    printf("\n %i %s ", i, word);
                    i++;
                             }

I can only suppose that the compiler(visual studio express) dosn't support such comparisions of sth should be done about the array's sizes.

#include <stdio.h>
#include <conio.h>

void main() {
    FILE *f;
    int i = 1;
    char word[20];
    char name[20];
    char search[20];
    printf("Please input the mane of the file: ");
    scanf("%s", name);
    if((f=fopen(name, "r"))==NULL) {
        printf("Error");
        getch();
                return;
    }
    printf ("\nPlease input the word for search: ");
    scanf("%s", search);
    while (!feof(f)) {
    fscanf(f, "%s", word);
        if (word==search){
        printf("\n %i %s ", i, word);
        i++;}
    }
    fclose(f);
    getch();
}

Recommended Answers

All 4 Replies

That is right you can directly compare C style string using ==. You need to use the library function strcmp

Thenks a lot for strcmp, now it works.

ps: but then another question:) If it's ok to use == in C, why it gives me an error?

Sorry that's a typo on my part should have read

"you can't directly compare C style string using =="

This explains:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.