【发布时间】:2012-10-11 12:15:58
【问题描述】:
(C PROGRAM) 我正在尝试编译一个使用标头的 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
main.c
#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);
};
frequence.h
#ifndef DEF_FREQUENCE
#define DEF_FREQUENCE
typedef struct stat_mot;
int count_word(char * , char * );
struct stat_mot show_all_words(char *);
#endif
frequence.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;
}
【问题讨论】:
-
另请注意,
main()在 C 中的有效声明是:int main(int argc, char *argv[])和int main(void)。除非,您正在使用一些特定于编译器的功能,允许您声明不同类型的main()。 -
顺便说一句,没有人关心你尝试了多少。这与问题无关。
标签: c linux gcc compiler-construction