【问题标题】:warning: passing argument 1 of ‘word_search’ from incompatible pointer type [enabled by default]警告:从不兼容的指针类型传递“word_search”的参数 1 [默认启用]
【发布时间】:2016-03-11 20:55:50
【问题描述】:

我对 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);
        }
}

提前谢谢你。

【问题讨论】:

  • 我知道您是 C 新手。但是您似乎有一些基本错误,即使是新手也可以看到如果您真的仔细检查了您的代码。例如,int word_search(char word[100], char str[100]); 声明了一个带有两个参数的函数,但您使用四个参数调用它:word_search(&amp;fileptr, word, &amp;length, &amp;num);
  • 并从char* word[100]; int *length; int *num;中删除*
  • 编码时,一致缩进,不要使用制表符进行缩进。在每个左大括号“{”后缩进。在每个右大括号 '}` 之前不缩进。建议每个缩进级别使用 4 个空格。永远不要使用制表符,因为每个文字处理器/编辑器的制表位/制表符宽度设置不同

标签: c string function pointers


【解决方案1】:

您的 wordsearh 函数使用 2 个参数定义:

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

你用 4 个参数调用;

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

您不想从 ma​​in 调用函数 search_and_count 吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-10
    • 2020-07-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多