警告:从不兼容的指针类型[默认启用]传递'word_search'的参数1

时间:2016-03-11 20:55:50

标签: c string function pointers

我是C编程的新手,我不断收到这些我不知道的错误:

HW5_mkhan44.c: In function ‘main’:
HW5_mkhan44.c:31:3: warning: passing argument 1 of ‘word_search’ from incompatible pointer type [enabled by default]
HW5_mkhan44.c:9:5: note: expected ‘char *’ but argument is of type ‘struct FILE **’
HW5_mkhan44.c:31:3: warning: passing argument 2 of ‘word_search’ from incompatible pointer type [enabled by default]
HW5_mkhan44.c:9:5: note: expected ‘char *’ but argument is of type ‘char **’
HW5_mkhan44.c:31:3: error: too many arguments to function ‘word_search’
HW5_mkhan44.c:9:5: note: declared here

此外,这是代码:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int word_search(char word[100], char str[100]);
int character_count(char curr_str[100]);
void search_count(FILE *fpter, char c[100], int *len, int *result);

int main(void)
{
        FILE *fileptr;
        char filename[100];
        char* word[100];
        int *length;
        int *num;

        printf("Please input the text file name: ");
        scanf("%s", filename);

        fileptr = fopen(filename, "r");



    if(fileptr == NULL)
                printf("File does not exist");
        else{
                printf("Please enter a word to search: ");
                scanf("%s", word);
                word_search(&fileptr, word, &length, &num);

                if(*num == 0){  
                        printf("Word exists in file");
                        printf("There are %d characters in file", *length);
                }
                else
                        printf("Word does not exist in file");
        }
        fclose(fileptr);

        return 0;
}

int word_search(char word[100], char str[100])
{
        int num;
        if(strcmp(word, str) == 0){
                num = 0;
                return(num);
        }
        else{
                num = 1;
                return(num);
        }
}

int character_count(char current_str[100])
{
        int word_length;

        word_length = strlen(current_str);

        return(word_length);
}

void search_and_count(FILE *fpter, char c[100], int *len, int *result)
{
        char line[120];

        while(fgets(line, sizeof(line), fpter)){
                char* t = strtok(line, " ");
                *len = wordsearch(c, t);
                *result = character_count(t);
        }
}

提前谢谢。

1 个答案:

答案 0 :(得分:1)

您的wordsearh函数定义了2个参数:

int word_search(char word[100], char str[100])

你打电话给4个参数;

word_search(&fileptr, word, &length, &num);

您不想从主要调用 search_and_count 功能?