-1

So I declared an array of strings as typedef char String[11]; and in my main function I have this condition `

char word[12];
String hashArray[SIZE];

if (insertword(word, hashArray) == 1)
  printf("word %s successfully inserted.\n", word); 

This is the function that it calls.

int insertword(char word[], String hashArray[]) 
{

   //get the hash 
   int hashIndex = hashfunction1(word)% 7 ; 
   printf("INDEX:%d\n", hashIndex); //delete later

   if (hashArray[hashIndex] == NULL) //ERROR IS HERE
   {
        strcpy(hashArray[hashIndex], word);
        printf("INSERTED!!  ");
        return 1;
   }
   else 
   {
        printf("NOT INSERTED!!  ");
        return 0;
   }
}

I have tested my program by adding a print and apparently the error happens at the first conditional statement. The else part is always what gets executed. I'm definitely missing something here, any help would be appreciated.

Erail
  • 169
  • 1
  • 8
  • Is `hashIndex` mandated to be *less than* `SIZE`? Otherwise ***Undefined Behavior*** results. And is `SIZE` always greater than `strlen (word) + 1;` (same result if not in `strcpy(hashArray[hashIndex], word);` if `hashArray[hashIndex]` doesn't provide at least `strlen (word) + 1;` chars of storage.) Are all `hashArray[hashIndex]` elements initialized to `NULL`? If so, you have no storage to `strcpy(hashArray[hashIndex], word);` because `hashArray[hashIndex]` is an ***Uninitialized/Unallocated*** pointer. – David C. Rankin Aug 06 '17 at 05:10
  • See: [**How to create a Minimal, Complete, and Verifiable example**](http://stackoverflow.com/help/mcve). – David C. Rankin Aug 06 '17 at 05:16
  • i have 32 strings from a text file and i made sure that enough space will be allocated for each word. i think the problem roots at the array being uninitialized. however i havent assigned anything to the array yet.. should i set the elements to null? i have a feeling it should already be empty from the moment i declared it. – Erail Aug 06 '17 at 05:37
  • 1
    You have a **mutually exclusive problem**. If `hashArray[hashIndex]` is an `array of pointers to char *` (which is hard to tell because you have typedeffed a pointer behind `String` in `String hashArray[SIZE];`). See: [Is it a good idea to **typedef** pointers](http://stackoverflow.com/questions/750178/is-it-a-good-idea-to-typedef-pointers). If they are in fact pointers, then they can be initialized `NULL`, but you cannot `strcpy(hashArray[hashIndex], word);` until you allocate memory to hold the string and assign the starting address to the block to `hashArray[hashIndex]`. – David C. Rankin Aug 06 '17 at 06:36
  • @DavidC.Rankin This gave me an idea of what the problem was, to solve it i declared my array ` char *hashArray[SIZE]; ` and allocated memory. Thank you! – Erail Aug 07 '17 at 09:02
  • 1
    Glad it helped. Once you get the hang of handling dynamically allocated memory, it's a piece of cake, but... It takes working with pointers enough to be comfortable with them before the ("I need 'p' to point to some block of memory") actually sinks in. Once you get the basics, and it looks like you have, handling dynamic allocations, doesn't change or get more complicated, your ability to adapt its use to wherever you need it will just become 2nd nature. – David C. Rankin Aug 08 '17 at 06:19

1 Answers1

1

The problem with your code:

char word[12];
String hashArray[SIZE];

word is not initialized, hence it has garbage values.
Then inside insertword function:

int hashIndex = hashfunction1(word)% 7; 

The array "decays" into a pointer to the first element:

hashfunction1(word)%7;

Is just as:

hashfunction1(word[0])%7;

When again, word is not initialized, so the value will be put in hashIndex is unknown!
Then you are accessing the array hashArray[hashIndex] in that index which is unknown, might be out of boundary.

Accessing an array outside its bounds has "undefined behavior".


To fix this, you need to put some random values in word array which will be smaller than 32, as stated in your comment, size = 32.

Tony Tannous
  • 14,154
  • 10
  • 50
  • 86