我正在编写一个程序,用于统计一个单词在文本文件中出现的次数。这是通过使用一个包含单词和该单词计数的结构来完成的。我得到了一个编译,我得到一个错误,上面写着:
"对不完整的类型结构wordcounter应用sizeof无效。"
任何关于如何解决这个问题以及我的代码的意见都将不胜感激。我是c的新手,所以我也愿意接受与这个问题无关的建议。非常感谢。我的代码是:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#define MAXWORDS 5000
#define MAXLINE 1000
#define MAXWORDLEN 100
int count = 0;
struct wordcount *wordPtr[MAXWORDS];
typedef struct wordcount *wordcountptr;
typedef struct wordcount {
char word[50];
int count;
} wordcount;
main()
{
int wordfound = 0;
int len;
char line[MAXLINE];
int printcount;
while ((len = getline(line, MAXLINE, stdin))> 0)
{
int i = 0;
int j = 0;
for( ; i<len; i++)
{
if(line[i] != isalnum(line[i]) && line[i] != 32)
line[i] = 32;
else
line[i] = tolower(line[i]);
}
for( ; j<len; j++)
{
char currentword[MAXWORDLEN];
if(line[j] != 32)
{
for(i=0; line[j] != 32; i++)
currentword[i] = line[j];
}
if(line[j] == 32)
{
for(i=0;i<MAXWORDS; i++)
{
if(strcmp(currentword, (*wordPtr[i]).word) == 0)
{
(*wordPtr[i]).count++;
wordfound = 1;
}
}
if(wordfound == 0)
{
wordPtr[i] = (struct wordcounter*)malloc(sizeof(struct wordcounter));
strcpy((*wordPtr[i]).word, currentword);
(*wordPtr[i]).count = 1;
count++;
}
}
wordfound = 0;
}
}
for(printcount = 0; printcount < count; printcount++)
printf("There are %d occurances of the word %sn", (*wordPtr[printcount]).count, (*wordPtr[printcount]).word);
for(printcount = 0; printcount < MAXWORDS; printcount++)
{
free((void*)wordPtr[printcount]);
wordPtr[printcount]= NULL;
}
}
typedef struct wordcount {
char word[50];
int count;
} wordcount_t;//modify like this
wordPtr[i] = (wordcount_t*)malloc(sizeof(wordcount_t));//modify like this