Ale*_*lex 1 c linux compiler-construction gcc
(C 程序)我正在尝试编译一个使用标头的 main.c,但出现以下错误。\n当我不使用标头(主文件中的所有方法)时,一切正常。
\n\n在字符串 S 中,程序查找所有出现的单词并返回出现次数最多的单词。
\n\n我正在编译使用:gcc main.c
谢谢。
\n\n错误
\n\nIn file included from main.c:9:0:\nfrequence.h:4:16: warning: useless storage class specifier in empty declaration [enabled by default]\nmain.c: In function \xe2\x80\x98main\xe2\x80\x99:\nmain.c:15:10: error: variable \xe2\x80\x98word\xe2\x80\x99 has initializer but incomplete type\nmain.c:15:10: warning: passing argument 1 of \xe2\x80\x98show_all_words\xe2\x80\x99 from incompatible pointer type [enabled by default]\nfrequence.h:6:17: note: expected \xe2\x80\x98char *\xe2\x80\x99 but argument is of type \xe2\x80\x98char (*)[34]\xe2\x80\x99\nmain.c:15:10: error: invalid use of undefined type \xe2\x80\x98struct stat_mot\xe2\x80\x99\nmain.c:15:19: error: storage size of \xe2\x80\x98word\xe2\x80\x99 isn\xe2\x80\x99t known\nRun Code Online (Sandbox Code Playgroud)\n\n主程序
\n\n#include <stdio.h>\n#include <stdlib.h>\n#include <string.h>\n#include "frequence.h"\n\n#define LONGUEURMAX 4\n\nint main(char *argv[]) {\n char texte[] = ";! one two, tree foor one two !:;";\n struct stat_mot word = show_all_words(&texte);\n\n printf("%s : %d\\n", word.word, word.frequency);\n\n return (EXIT_SUCCESS);\n};\nRun Code Online (Sandbox Code Playgroud)\n\n频率.h
\n\n#ifndef DEF_FREQUENCE\n#define DEF_FREQUENCE\n\ntypedef struct stat_mot;\nint count_word(char * , char * );\nstruct stat_mot show_all_words(char *);\n\n#endif\nRun Code Online (Sandbox Code Playgroud)\n\n频率.c
\n\n#include "frequence.h"\n\ntypedef struct stat_mot {\n char * word;\n int frequency;\n} stat_mot;\n\nint count_word(char * mot, char * text) {\n int n = 0;\n char *p;\n\n p = strstr(text, mot); \n while (p != NULL) {\n n++;\n p = strstr(p + 1, mot);\n } \n\n return n;\n}\n\nstat_mot show_all_words(char * text) {\n char * text_rw = strdup(text);\n char * p = strtok(text_rw, " .,;:-!?");\n\n char word_to_return[strlen(text)];\n int word_frequence = 0;\n\n while (p != NULL) {\n if (strlen(p) >= 0) {\n int offset = p - text;\n int count = count_word(p, text);\n\n if (word_frequence < count) {\n strcpy(word_to_return, p); \n word_frequence = count;\n }\n };\n\n p = strtok(NULL, " .,;:-!?");\n }\n\n free(text_rw);\n struct stat_mot word = { word_to_return, word_frequence };\n return word;\n}\nRun Code Online (Sandbox Code Playgroud)\n
至少,您需要stat_mot将 frequence.c 的定义移至 frequence.h。main()尝试创建它的实例,如果没有结构定义,则无法执行此操作。
还有一些其他问题:
char*我发现很难相信和之间的类型不匹配char(*)[34]是通过将所有内容放入一个文件来解决的。因此,我希望修复与您将代码组织到文件中无关,只是您应该通过texte,而不是&texte。typedef是一个存储类说明符。别问为什么。基本上,typedef struct stat_mot;是错误的,但没关系,因为您可以在移动结构定义时将其删除。