0

Hello i am trying to find the frequncey of words in tow dimensional arry but im having problem with my codes so far this is what i have done i tried to find whats wrong using debugger but i cant find anything usefull im new to coding so i would be really happy if someone can tell me the problem

I pointed out where im having problem in my code

    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    void freeFunction(char** arry, int size);
    void insertArry(char** words, int countWords);
    void wordFrequency(char** words, int countWords);
    
    int main()
    {
        char** words=NULL;
        int count = NULL;
        insertArry(words, &count);
    wordFrequency(words,count);
}
void insertArry(char** words, int* countWords)
{
    char buffer[11];
    int numberC;
    printf("How many words?: ");
    scanf("%d", &numberC);
    getchar();
    words = (char**)malloc(numberC * sizeof(char*));
    if (words == NULL)
    {
        printf("No memory");
        exit(1);
    }
    for (int i = 0; i < numberC; i++)
    {

        printf("Enter word: ");
        gets_s(buffer, 11);
        words[i] = (char*)malloc(strlen(buffer) + 1);
        if (words == NULL)
        {
            freeFunction(words, i);
            exit(1);
        }

        strcpy(words[i], buffer);
    }
    *countWords = numberC;
}
 
void freeFunction(char** arry, int size){
    printf("No memory");
    for (int j = 0; j < size; j++)
    {
        free(arry[j]);
    }
    free(arry);
}
void wordFrequency(char** words, int countWords)
{
    
    int counter = 0;
    for (int i = 0; i < countWords; i++) {
        
        for (int j = i + 1; j < countWords; j++)
        {
            if(strcmp(words[i],words[j])==0) /// ***THIS IS WHERE MY PROBLEM RELAY ITS GIVING ME WEIRD ADDRESSES***
                counter += 1;
        }
        printf("%s:%d",words[i], counter);
        counter = 0;
    }
}
Nath
  • 13
  • 4
  • Does this answer your question? [How do I properly compare strings in C?](https://stackoverflow.com/questions/8004237/how-do-i-properly-compare-strings-in-c) – kaylum May 05 '21 at 00:00
  • @kaylum no i know how to do it with out pointers my problem doing it with pointers but thanks tho – Nath May 05 '21 at 00:03
  • What do you mean? A string in C is always essentially a pointer. So `if (words[i] = words[j])` needs to be `if (strcmp(words[i], words[j]) == 0)`. Explained in the duplicate post. – kaylum May 05 '21 at 00:05
  • @kaylum i know that string is essentially a pointer but here i did ur way but still its giving me address problems its like its sending different address that has no values its like sending NULL Address – Nath May 05 '21 at 00:07
  • `words = (char**)malloc(numberC * sizeof(char*));` doesn't work. All parameters in C are passed by value. So `words` is a local variable in the function and setting it does not set the caller's value: [Changing address contained by pointer using function](https://stackoverflow.com/questions/13431108/changing-address-contained-by-pointer-using-function). – kaylum May 05 '21 at 00:11
  • @kaylum your explaining to me like i have been coding for years didn't understand anything of what you have said what you mean like the value that im scanning to this array is not being saved? – Nath May 05 '21 at 00:14
  • That is why I gave you a link. Please read that. – kaylum May 05 '21 at 00:19
  • @kaylum but i don't want to change address and the one you sent me they want to change addresses of variables but ill see read it all just because u said so hope it doesn't waste my time but thanks – Nath May 05 '21 at 00:23
  • It's terminology that may be confusing you but you are indeed trying to "change address contained by pointer". That's what `word = malloc()` does. `malloc` returns an address and you are trying to assign it to `word`. But `word` is a local variable and no longer exists when the function exits. The caller's `word` variable is not changed as you need it to be. That's what the link explains and shows what needs to be done to make it work. – kaylum May 05 '21 at 00:50
  • @kaylumok now I understood you thanks a lot for if I wasted your time – Nath May 05 '21 at 12:52
  • 1
    @kaylum Hey thanks bro i read what you sent and you were right it fixed my problem thanks for the answers – Nath May 06 '21 at 01:11

0 Answers0