我收到了一堆指針錯誤,指針並不完全是我最強烈的一點。我的大部分錯誤都與傳遞參數有關。從不兼容指針類型[默認啓用]傳遞參數2'search_and_count'
的錯誤是:
HW5_mkhan44.c:在函數 '主':HW5_mkhan44.c:31:3:警告: 從不相容指針 類型傳遞 'search_and_count' 的參數1 [由啓用默認]
HW5_mkhan44.c:11:6:注:預期 '結構FILE *',但參數是 類型 '結構FILE **' 的
HW5_mkhan44.c:31:3:警告:傳遞參數2'search_and_count' 從兼容的指針類型[默認啓用]
HW5_mkhan44.c:11:6:注:預期 '爲const char *',但參數是 類型的 '字符*(*)[100]'
HW5_mkhan44.c:31:3:警告:傳遞來自兼容的指針類型 'search_and_count' 的參數3 [默認啓用]
HW5_mkhan44.c:11:6:注:預期 '詮釋*',但參數是 'int **'
HW5_mkhan44.c:31:3:warnin G:從兼容的指針類型傳遞的 'search_and_count' 參數4 [啓用默認]
HW5_mkhan44.c:11:6:注:預期 '詮釋*',但參數是 類型的 '詮釋**'
這是代碼:
#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);
}
}
Upvoted,但建議稍作修改:「'char []'因爲它已經是一個指向char []的指針,因爲它會衰減到指針」或類似的地方。數組不是一個指針,但可以像一個對待。更多在這裏:http://stackoverflow.com/questions/1461432/what-is-array-decaying – user4581301