Я пишу программу, которая подсчитывает, сколько раз слово встречается в текстовом файле. Это делается с помощью структуры, которая содержит слово и количество этого слова. Я получаю компиляцию, я получаю сообщение об ошибке:
«Неверное применение 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 %s\n", (*wordPtr[printcount]).count, (*wordPtr[printcount]).word);
for(printcount = 0; printcount < MAXWORDS; printcount++)
{
free((void*)wordPtr[printcount]);
wordPtr[printcount]= NULL;
}
}
wordcount
!=wordcounter
. Сравните свое имя decl с именем, которое вы использовали в коде. Там больше, чем один, кстати. - person WhozCraig   schedule 04.12.2013