0

Good evening everyone! I have started messing around with strings and pointers in C. I want to write a programm that reads a text file, then calculating the frequency of each word and printing it.

My variables are:

FILE *fp;
char *words[N] //N defined 100
int i=0, y=0;
int *freq;
int freq_count=0;;
int word_number=0;

The code part:

for(i=0;i<word_counter;i++){
    while(y<word_counter){
         if(strcmp(words[i],words[y]==0){
             freq1++;
         } y++;
    }
    if(i==0){
         freq=(int*)malloc(sizeof(int));
         strcpy(freq, freq1); freq1=0;
    }
    else{
         freq=(int*)realloc(freq, (i+1)*sizeof(int));
         strcpy(freq, freq1); freq1=0;
    }
    y=0;
}

I get several errors running this...What is wrong? Take into consideration that in words[N] i have put each word of the text by itself in each cell.

Thank you all in advance.

  • 1
    `freq(int*)realloc(freq, (i+1)*sizeof(int));`???? – Sourav Ghosh Jan 16 '16 at 15:51
  • Don't use `strcpy` for `int` and `int*`. – BLUEPIXY Jan 16 '16 at 15:54
  • Could you please tell me how to fix it cause i'm clueless... – VasilisDovas Jan 16 '16 at 15:54
  • Welcome to Stack Overflow! [Please see this discussion on why not to cast the return value of `malloc()` and family in `C`.](http://stackoverflow.com/q/605845/2173917). – Sourav Ghosh Jan 16 '16 at 16:03
  • i will take a look at it, thank you – VasilisDovas Jan 16 '16 at 16:14
  • Instead of `strcpy(freq, freq1);`, use `freq[i] = freq1;` – Spikatrix Jan 16 '16 at 16:14
  • It may be good to be sorted prior to count. – BLUEPIXY Jan 16 '16 at 16:39
  • Do you get those "several errors" when you **run** it or when you try to **compile** the source? There are several _typos_ – mauro Jan 16 '16 at 16:50
  • when calling the memory allocation functions: `malloc()`, `realloc()` and `calloc()`, 1) the returned type is `void*` so can assigned to any pointer. casting the returned value just clutters the code, making it more difficult to understand, debug, maintain. 2) Always check (!= NULL) the returned value to assure the operation was successful. 3) when calling `realloc()` always save to a temporary pointer, then check != NULL, and if not NULL, then assign to the target pointer. Otherwise, if `realloc()` fails, which it can do, then the original pointer is lost, resulting in a memory leak. – user3629249 Jan 16 '16 at 23:51
  • when asking a question about a compile problem,. do not make us guess about which headers, etc are included, post a function, not some random snippet of code. When asking about a run time problem, post code that cleanly compiles, is minimal and still exhibits the problem. – user3629249 Jan 16 '16 at 23:54
  • when the code displays error, post the errors. Otherwise, especially with some small snippet of code, we can only guess as to the root of the problem. AND how are we to advise on how to fix an error if you do not post the error? – user3629249 Jan 16 '16 at 23:56

1 Answers1

1

Maybe another array is not what you want, but still better than using realloc and condition in loop.

int freq[N];

for(i=0;i<word_counter;i++){
    freq1 = 0; 
    for(y=0;y<word_counter;y++){
        if(strcmp(words[i],words[y]==0)
            freq1++;
    }
    freq[i] = freq1;
}
Enkelli
  • 305
  • 5
  • 18