0

I am making an anagram program that takes a word and finds its anagrams then returns the list of anagrams from a given dictionary file. I am using a linked list as a hash map and a list for each node of the hash map (as per my assignment). I am pretty new to C so I am having a lot of issues with memory and pointers.

Some of the common errors I am getting when I compile: warning: initialization makes pointer from integer without a cast list *pList = getListFromMap(key); I don't understand this one, pList is a list pointer and getListFromMap is returning a list pointer.

conflicting types for getListFromMap() list *getListFromMap(int key) {

previous delcaration of getListFromMap was here getListFromMap(key); I read something about forward declaration? Though I am not sure how this works, I got an error trying it.

 typedef struct list {
     char *word;
     struct list *next;
 } list;

 typedef struct hashmap {
     list *words;
     int key;
     struct hashmap *next;
 } hashmap;

hashmap *pMapHead;
hashmap *pMapCurr;
list *pListHead;
list *pListCurrd;
int sum = 0; // sum of words
int hCount = 0;

void addWordsToMap(int key, list *words) { // create hashmap

    hashmap *pHash = pMapHead;
    pMapCurr = pHash;
    while (pMapCurr) {
            pMapCurr = pMapCurr->next;
    }
    pMapCurr->words = words;
    pMapCurr->key = key;
    pMapCurr->next = NULL;
    hCount += 1;
}


list *addWord(int key) {
    pListHead = getListFromMap(key); 
    pListCurr = pListHead;
    while (pListCurr) {
        pListCurr = pListCurr->next;    
    }
    pList->word = word;
    pList->next = NULL;
    pCurr->next = pList;
    pCurr = pList;

    return pListHead;
}

list *getListFromMap(int key) {
    hashmap *map = pMapHead;
    pMapCurr = map;
    while (pMapCurr != NULL) {
        if (pMapCurr->key == key) {
            return pMapCurr->words;
            free(map);
        }
        pMapCurr = pMapCurr->next;
    }
}

  int getSum(char* word) {
    int sum = 0;
    int i = 0;
    while (word[i]) {
        word[i] = toupper(word[i]);
        sum += word[i] - '@';
        i++;
    }
    return sum;
}

void loadWords() {
    FILE* dict = fopen("/home/akw54/Desktop/CS283/akw54-cs283-            summer2016/A1/dict", "r");
    if (dict == NULL) {
        return;
    }

    pListHead = (list *) malloc(sizeof(pListHead));
    pListCurr = pListHead;
    pMapHead = (hashmap *) malloc(sizeof(pMapHead));
    pMapCurr = pMapHead;
    int key = 0;    
    list wordList;
    char word[128];
    while(fgets(word, sizeof(word), dict) != NULL) {
        key = getSum(word);
        addWordsToMap(key, addWord(key));
    }   
    free(dict);
}

void main() {
    loadWords();
    free(pMapHead);
    free(pMapCurr);
    free(pListHead);
    free(pListCurr);
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
Adam Wolf
  • 309
  • 1
  • 4
  • 12
  • 1
    For starters, make sure you have at least a function prototype appearing before any use of the function in your code. – Dmitri Jul 10 '16 at 19:21
  • 1
    Your call to free() after getListFromMap returns is never executed, which is probably a good thing. The fact that it doesn't always return something isn't a good thing. – Scott Hunter Jul 10 '16 at 19:25
  • One of the many, *many* occurrences of this question [can be seen **here**](https://stackoverflow.com/questions/13314049/warning-initialization-makes-pointer-from-integer-without-a-cast). – WhozCraig Jul 10 '16 at 19:25

1 Answers1

3

The reason that you get "initialization makes pointer from integer" warning, despite the function returning a pointer, is somewhat obscure. The root case is that the function has no prototype, but that's not the whole story. According to pre-C99 rules, you were allowed to call a function that has no forward declaration, but the compiler must assume that all parameters are subject to default type promotions, and the return type is int as well.

That is why when the compiler sees pListHead = getListFromMap(key) call, it assumes that the function returns an int. In order to avoid this problem, add a forward declaration at the top of the file, right after the typedefs:

list *getListFromMap(int key);
Community
  • 1
  • 1
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Thanks man that worked! When I tried the forward declaration earlier I was just doing getListFromMap(int key); Obviously wrong now. – Adam Wolf Jul 10 '16 at 19:35