Пиша програма, която отчита колко пъти се появява дума в текстов файл. Това става чрез използването на структура, която съдържа думата и броя на тази дума. Получавам компилация. Получавам грешка, която гласи:
„Невалидно приложение на sizeof към непълен тип struct wordcounter.“
Всяка информация за това как да се коригира този проблем и за моя код като цяло се оценява. Нов съм в 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