(C 程序)我正在尝试编译使用标头的 main.c,但出现以下错误。当我不使用标题(主文件中的所有方法)时,一切正常。
在字符串 S 中,程序查找所有出现的单词并返回出现次数最多的单词。
我正在编译使用:gcc main.c
谢谢你。
错误
In file included from main.c:9:0:
frequence.h:4:16: warning: useless storage class specifier in empty declaration [enabled by default]
main.c: In function ‘main’:
main.c:15:10: error: variable ‘word’ has initializer but incomplete type
main.c:15:10: warning: passing argument 1 of ‘show_all_words’ from incompatible pointer type [enabled by default]
frequence.h:6:17: note: expected ‘char *’ but argument is of type ‘char (*)[34]’
main.c:15:10: error: invalid use of undefined type ‘struct stat_mot’
main.c:15:19: error: storage size of ‘word’ isn’t known
主程序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "frequence.h"
#define LONGUEURMAX 4
int main(char *argv[]) {
char texte[] = ";! one two, tree foor one two !:;";
struct stat_mot word = show_all_words(&texte);
printf("%s : %d\n", word.word, word.frequency);
return (EXIT_SUCCESS);
};
频率.h
#ifndef DEF_FREQUENCE
#define DEF_FREQUENCE
typedef struct stat_mot;
int count_word(char * , char * );
struct stat_mot show_all_words(char *);
#endif
频率.c
#include "frequence.h"
typedef struct stat_mot {
char * word;
int frequency;
} stat_mot;
int count_word(char * mot, char * text) {
int n = 0;
char *p;
p = strstr(text, mot);
while (p != NULL) {
n++;
p = strstr(p + 1, mot);
}
return n;
}
stat_mot show_all_words(char * text) {
char * text_rw = strdup(text);
char * p = strtok(text_rw, " .,;:-!?");
char word_to_return[strlen(text)];
int word_frequence = 0;
while (p != NULL) {
if (strlen(p) >= 0) {
int offset = p - text;
int count = count_word(p, text);
if (word_frequence < count) {
strcpy(word_to_return, p);
word_frequence = count;
}
};
p = strtok(NULL, " .,;:-!?");
}
free(text_rw);
struct stat_mot word = { word_to_return, word_frequence };
return word;
}