gpt4 book ai didi

c - 从不兼容的指针类型传递 ‘search_and_count’ 的参数 2 [默认启用]

转载 作者:太空宇宙 更新时间:2023-11-04 05:52:39 27 4
gpt4 key购买 nike

我遇到了一堆指针错误,而且指针并不是我的强项。我的大部分错误都与传递参数有关。

错误是:

HW5_mkhan44.c: In function ‘main’: HW5_mkhan44.c:31:3: warning: passing argument 1 of ‘search_and_count’ from incompatible pointer type [enabled by default]

HW5_mkhan44.c:11:6: note: expected ‘struct FILE *’ but argument is of type ‘struct FILE **’

HW5_mkhan44.c:31:3: warning: passing argument 2 of ‘search_and_count’ from incompatible pointer type [enabled by default]

HW5_mkhan44.c:11:6: note: expected ‘const char *’ but argument is of type ‘char * (*)[100]’

HW5_mkhan44.c:31:3: warning: passing argument 3 of ‘search_and_count’ from incompatible pointer type [enabled by default]

HW5_mkhan44.c:11:6: note: expected ‘int *’ but argument is of type ‘int **’

HW5_mkhan44.c:31:3: warning: passing argument 4 of ‘search_and_count’ from incompatible pointer type [enabled by default]

HW5_mkhan44.c:11:6: note: expected ‘int *’ but argument is of type ‘int **’

这是代码:

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

int word_search(const char *word, const char *str);
int character_count(const char *curr_str);
void search_and_count(FILE *fpter, const char *c, 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\n");
else{
printf("Please enter a word to search: ");
scanf("%s", word);
search_and_count(&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(const char *word,const char *str)
{
int num;
if(strcmp(word, str) == 0){
num = 0;
return(num);
}
else{
num = 1;
return(num);
}
}

int character_count(const char *current_str)
{
int word_length;

word_length = strlen(current_str);

return(word_length);
}

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

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

最佳答案

这看起来像是语法错误:

  • search_and_count(&fileptr, &word, &length, &num);你已经将它们声明为指针,所以删除所有 & s 如果你想发送指针
  • char* []声明应该只是 char []因为它可以被视为指针

然后遍历每个语句,看看是否有任何逻辑错误,祝你好运!

关于c - 从不兼容的指针类型传递 ‘search_and_count’ 的参数 2 [默认启用],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35951533/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com