fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define WORD_SIZE 1024
  6.  
  7. typedef struct data_{
  8. char *key;
  9. void *data;
  10. struct data_ *next;
  11. }data_el;
  12.  
  13. typedef struct hash_table_ {
  14. /* structure definition goes here */
  15. data_el **buckets_array;
  16. } hash_table, *Phash_table;
  17.  
  18. typedef struct word_{
  19. char key[WORD_SIZE];
  20. int frequency;
  21. } word;
  22.  
  23. int main()
  24. {
  25.  
  26. int size=0;
  27. word *new_word;
  28. new_word = (word *)malloc(sizeof(word));
  29.  
  30. new_word->frequency = 5;
  31. strcpy(new_word->key, "Lalalal");
  32. // ^ ^ ^ ^ - ooops! you need to copy lalalal bytes into key[] array
  33.  
  34. Phash_table table_p;
  35. table_p = (Phash_table)malloc(sizeof(hash_table));
  36. table_p->buckets_array = (data_el **)malloc(sizeof(data_el *)*(size+1));
  37.  
  38.  
  39. table_p->buckets_array[0] = (data_el *)malloc(sizeof(data_el));
  40. // ^ ^ ^ ^ - ooops! you might need some data_el lol
  41.  
  42. table_p->buckets_array[0]->data = (void*)new_word;
  43.  
  44.  
  45. /*Is this right? How do I cast this? (word*) ?*/
  46. word* pdata=table_p->buckets_array[0]->data;
  47. // ^ ^ ^ ^ - the readable way
  48.  
  49. printf("Key :%s Frequency:%d ",((word*)(table_p->buckets_array[0]->data))->key, //<--the insane cast way (promote void* data_el.data to a word*)
  50. pdata->frequency);
  51.  
  52. return 0;
  53. }
Success #stdin #stdout 0.01s 1852KB
stdin
Standard input is empty
stdout
Key :Lalalal Frequency:5