I am relatively new to programming, and this is my first term working in C. So it is entirely possible that this could be a really simple mistake, or it is also possible that the explanation of what I did wrong could go over my head.
My program reads in a text file a builds a concordance in a hashmap which tells the user which words are in the text and how many times they occur. However, I continue to segfault (code 11), whenever I use the myCompare function. Here is its code:
int myCompare(void *s1, void *s2)
{
printf("\n\nStarting myCompare...");
printf("\n*s1: %s", (char *)s1);
printf("\n*s2: %s", (char *)s2);
char *key1 = (char *)s1;
printf("\n\nkey1: %s", key1);
char *key2 = (char *)s2;
printf("\nkey2: %s", key2);
return (strcmp(key1, key2));
}
I added some print statements to it and the function that calls it to give me a better idea of what is passing and exactly where it is segfaulting, but I am not sure why it is happening. Here is the output, if anyone needs more of the code to understand my mistake, just let me know. Thanks guys
struct hashLink {
void* key;
void* value;
struct hashLink * next;
};
struct hashMap {
hashLink ** table;
int tableSize;
int count;
};
...
void removeKey (struct hashMap * ht, void* k, comparator keyCompare, hashFuncPtr hashFunc)
{
struct hashLink *previousLink;
struct hashLink *currentLink;
int index = 0;
printf("\n\nVariables Declared");
printf("\nPre-hash index: %d", index);
index = ((*hashFunc)(k) % ht->tableSize);
void* keyPtr = &ht->table[index]->key;
printf("\nPost-hash index: %d", index);
printf("\nkeyPtr value: %p", keyPtr);
printf("\nk value: %p", k);
int testCompare = (*keyCompare)(k, keyPtr);
printf("\nkeyCompare: %d", testCompare);
if ((*keyCompare)(k, keyPtr) ==)){
... }
Terminal Output:
Starting hash2...
Passed key: and
value of r: 617
Post-hash index: 7
keyPtr value: 0x10e65dcd8
k value: 0x10e65df24
Starting myCompare...
*s1: and
*s2:
key1: and
key2:
keyCompare: 65
Starting myCompare...
*s1:
*s2: and
key1:
Segmentation fault: 11
logout