C - How to assign multiple values to the same key in a hashmap?









up vote
2
down vote

favorite












As a novice programmer I started reading through The C Programming Language to learn more about pointers and structs.



I am currently learning about hashmaps in C. Following the example in the book, I created my own hashmap to hold key-value pairs:



#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_STRING_SIZE 100
#define HASHSIZE 101

static struct map *hashtab[HASHSIZE]; /* pointer table */
unsigned hash(char *); /* hashing function: form hash value for string s. used by both lookup and install*/
char *strdup(char *);

struct map /* creating a map structure */
struct map *next;
char *KEY; /* KEY - pointer to a char - member of nlist*/
char *object1; /* object - pointer to a char - member of nlist*/
;

/* lookup function takes a pointer to char - s as an argument and returns a pointer to map structure */
struct map *lookup(char *s)
struct map *np;

for (np = hashtab[hash(s)]; np != NULL; np = np->next)
if (strcmp(s, np->KEY) == 0)
return np;


return NULL;


/* install function takes a pointer to a char - KEY, object and returns a pointer to map structure */
struct map *install(char *KEY, char *object1)
/* install: put (name, defn) in */
/* Install uses lookup to determine whether the KEY being installed
is already present. Proceeds to create a new entry or update*/
struct map *np;
unsigned hashval;

if ((np = lookup(KEY)) == NULL)
else
free((void *)np->object1);

if ((np->object1 = strdup(object1)) == NULL)
return NULL;

return np;



I then assign values to keys as follows:



int main(void) 

struct map *table[4] =
(install("key1", "value1")),
(install("key2", "value2")),
(install("key3", "value3")),
(install("key4", "value4"))
;

int i;

for (i = 0; i < 4; i++)
printf("%s->%sn", table[i]->KEY, table[i]->object);

printf("n");
return 0;



The code above works well, I am able to assign value1, ... , value4 to key1, ... ,key4 respectively. However, this does not allow me to assign multiple values to the same key.



Let's say I read the following from a text file:



key1 9000 600 Test1
key2 2000 600 Test2
key3 3000 120 Test3
key4 4000 120 Test4
.
.
key10 1000 560 Test10


I want to be able to store each key and assign multiple values to it. Since the number of columns is fixed, maybe I could have a structure that represents a row and put that into the map.



To do so, install needs to be modified to be able to add multiple values for the same key. Since the keys are unique, lookup and uninstall (function I created to remove keys) should remain unchanged.



The code above works perfectly, however I am looking for a general solution to add multiple values to the same keys.



How should I proceed to be able to call:



struct map *table[4] = 
(install("key1", "9000" ,"600", "Test1")), //key, object1, object2, object3
(install("key2", "2000" ,"600", "Test2")),
(install("key3", "3000" ,"120", "Test3")),
(install("key4", "4000" ,"120", "Test4"))
;


Another idea would be:



/* key points to this struct */
struct Value
int i;
int k;
char *c;
;

typedef struct map /* creating a map structure */
struct map *next;
char *KEY;
struct Value value; /* place multiple values inside a struct*/
;


and here is where I am getting stuck:



struct map *insert(char *KEY, struct *Value) 
struct map *np;
unsigned hashval;

if ((np = lookup(KEY)) == NULL)
else
free((struct Value*)np->value); //type cast cannot convert from 'Value' to 'Value*'


if ((np->Value = strdup(Value)) == NULL) //map has no field value
return NULL;


return np;



I searched through the following questions, however was unable to extract the relevant information on how to implement this in C.



Hashmaps having multiple keys with multiple values



How can I assign multiple values to a hash key?



HashMap with multiple valued keys



How can I implement a hashmap which accepts multiple values assigned to the same key?




EDIT



As pointed out in the comments, the structure I am using may not be actuallly a hashmap but a linked list. However the book specifically says that page 144 is a hashmap.










share|improve this question



















  • 2




    Hint: when inserting, treat any duplicate keys exactly as you treat collisions.
    – n.m.
    Nov 11 at 14:15










  • Thanks will look into it. Also, why is Implementing a hashmap with multiple values pointing to the same key considered broad? How can I improve my question?
    – Rrz0
    Nov 11 at 14:26










  • Your structure here is not actually a hash nap, it is a linked list. There are a number of ways you could go to accomplish your objective, but the traditional would be: actually implement a hash nap, and then when you have a key collision, implement a linked list of objects under that key.
    – bluejack
    Nov 11 at 14:33










  • Thanks for the comment. I fail to understand why I am expecting key collisions with such a small number of keys as in my example (10). Also why does the book I am referring to call the structure a hasmap? That is what confused me.
    – Rrz0
    Nov 11 at 14:37







  • 2




    An actual hash table or hash map associates each key with exactly one value. How else could one reasonably retrieve objects from such a data structure? But the one value can be of whatever type you choose, and in particular, and can be a type that can contain multiple other objects, such as a linked list (or the head of one).
    – John Bollinger
    Nov 11 at 18:00














up vote
2
down vote

favorite












As a novice programmer I started reading through The C Programming Language to learn more about pointers and structs.



I am currently learning about hashmaps in C. Following the example in the book, I created my own hashmap to hold key-value pairs:



#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_STRING_SIZE 100
#define HASHSIZE 101

static struct map *hashtab[HASHSIZE]; /* pointer table */
unsigned hash(char *); /* hashing function: form hash value for string s. used by both lookup and install*/
char *strdup(char *);

struct map /* creating a map structure */
struct map *next;
char *KEY; /* KEY - pointer to a char - member of nlist*/
char *object1; /* object - pointer to a char - member of nlist*/
;

/* lookup function takes a pointer to char - s as an argument and returns a pointer to map structure */
struct map *lookup(char *s)
struct map *np;

for (np = hashtab[hash(s)]; np != NULL; np = np->next)
if (strcmp(s, np->KEY) == 0)
return np;


return NULL;


/* install function takes a pointer to a char - KEY, object and returns a pointer to map structure */
struct map *install(char *KEY, char *object1)
/* install: put (name, defn) in */
/* Install uses lookup to determine whether the KEY being installed
is already present. Proceeds to create a new entry or update*/
struct map *np;
unsigned hashval;

if ((np = lookup(KEY)) == NULL)
else
free((void *)np->object1);

if ((np->object1 = strdup(object1)) == NULL)
return NULL;

return np;



I then assign values to keys as follows:



int main(void) 

struct map *table[4] =
(install("key1", "value1")),
(install("key2", "value2")),
(install("key3", "value3")),
(install("key4", "value4"))
;

int i;

for (i = 0; i < 4; i++)
printf("%s->%sn", table[i]->KEY, table[i]->object);

printf("n");
return 0;



The code above works well, I am able to assign value1, ... , value4 to key1, ... ,key4 respectively. However, this does not allow me to assign multiple values to the same key.



Let's say I read the following from a text file:



key1 9000 600 Test1
key2 2000 600 Test2
key3 3000 120 Test3
key4 4000 120 Test4
.
.
key10 1000 560 Test10


I want to be able to store each key and assign multiple values to it. Since the number of columns is fixed, maybe I could have a structure that represents a row and put that into the map.



To do so, install needs to be modified to be able to add multiple values for the same key. Since the keys are unique, lookup and uninstall (function I created to remove keys) should remain unchanged.



The code above works perfectly, however I am looking for a general solution to add multiple values to the same keys.



How should I proceed to be able to call:



struct map *table[4] = 
(install("key1", "9000" ,"600", "Test1")), //key, object1, object2, object3
(install("key2", "2000" ,"600", "Test2")),
(install("key3", "3000" ,"120", "Test3")),
(install("key4", "4000" ,"120", "Test4"))
;


Another idea would be:



/* key points to this struct */
struct Value
int i;
int k;
char *c;
;

typedef struct map /* creating a map structure */
struct map *next;
char *KEY;
struct Value value; /* place multiple values inside a struct*/
;


and here is where I am getting stuck:



struct map *insert(char *KEY, struct *Value) 
struct map *np;
unsigned hashval;

if ((np = lookup(KEY)) == NULL)
else
free((struct Value*)np->value); //type cast cannot convert from 'Value' to 'Value*'


if ((np->Value = strdup(Value)) == NULL) //map has no field value
return NULL;


return np;



I searched through the following questions, however was unable to extract the relevant information on how to implement this in C.



Hashmaps having multiple keys with multiple values



How can I assign multiple values to a hash key?



HashMap with multiple valued keys



How can I implement a hashmap which accepts multiple values assigned to the same key?




EDIT



As pointed out in the comments, the structure I am using may not be actuallly a hashmap but a linked list. However the book specifically says that page 144 is a hashmap.










share|improve this question



















  • 2




    Hint: when inserting, treat any duplicate keys exactly as you treat collisions.
    – n.m.
    Nov 11 at 14:15










  • Thanks will look into it. Also, why is Implementing a hashmap with multiple values pointing to the same key considered broad? How can I improve my question?
    – Rrz0
    Nov 11 at 14:26










  • Your structure here is not actually a hash nap, it is a linked list. There are a number of ways you could go to accomplish your objective, but the traditional would be: actually implement a hash nap, and then when you have a key collision, implement a linked list of objects under that key.
    – bluejack
    Nov 11 at 14:33










  • Thanks for the comment. I fail to understand why I am expecting key collisions with such a small number of keys as in my example (10). Also why does the book I am referring to call the structure a hasmap? That is what confused me.
    – Rrz0
    Nov 11 at 14:37







  • 2




    An actual hash table or hash map associates each key with exactly one value. How else could one reasonably retrieve objects from such a data structure? But the one value can be of whatever type you choose, and in particular, and can be a type that can contain multiple other objects, such as a linked list (or the head of one).
    – John Bollinger
    Nov 11 at 18:00












up vote
2
down vote

favorite









up vote
2
down vote

favorite











As a novice programmer I started reading through The C Programming Language to learn more about pointers and structs.



I am currently learning about hashmaps in C. Following the example in the book, I created my own hashmap to hold key-value pairs:



#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_STRING_SIZE 100
#define HASHSIZE 101

static struct map *hashtab[HASHSIZE]; /* pointer table */
unsigned hash(char *); /* hashing function: form hash value for string s. used by both lookup and install*/
char *strdup(char *);

struct map /* creating a map structure */
struct map *next;
char *KEY; /* KEY - pointer to a char - member of nlist*/
char *object1; /* object - pointer to a char - member of nlist*/
;

/* lookup function takes a pointer to char - s as an argument and returns a pointer to map structure */
struct map *lookup(char *s)
struct map *np;

for (np = hashtab[hash(s)]; np != NULL; np = np->next)
if (strcmp(s, np->KEY) == 0)
return np;


return NULL;


/* install function takes a pointer to a char - KEY, object and returns a pointer to map structure */
struct map *install(char *KEY, char *object1)
/* install: put (name, defn) in */
/* Install uses lookup to determine whether the KEY being installed
is already present. Proceeds to create a new entry or update*/
struct map *np;
unsigned hashval;

if ((np = lookup(KEY)) == NULL)
else
free((void *)np->object1);

if ((np->object1 = strdup(object1)) == NULL)
return NULL;

return np;



I then assign values to keys as follows:



int main(void) 

struct map *table[4] =
(install("key1", "value1")),
(install("key2", "value2")),
(install("key3", "value3")),
(install("key4", "value4"))
;

int i;

for (i = 0; i < 4; i++)
printf("%s->%sn", table[i]->KEY, table[i]->object);

printf("n");
return 0;



The code above works well, I am able to assign value1, ... , value4 to key1, ... ,key4 respectively. However, this does not allow me to assign multiple values to the same key.



Let's say I read the following from a text file:



key1 9000 600 Test1
key2 2000 600 Test2
key3 3000 120 Test3
key4 4000 120 Test4
.
.
key10 1000 560 Test10


I want to be able to store each key and assign multiple values to it. Since the number of columns is fixed, maybe I could have a structure that represents a row and put that into the map.



To do so, install needs to be modified to be able to add multiple values for the same key. Since the keys are unique, lookup and uninstall (function I created to remove keys) should remain unchanged.



The code above works perfectly, however I am looking for a general solution to add multiple values to the same keys.



How should I proceed to be able to call:



struct map *table[4] = 
(install("key1", "9000" ,"600", "Test1")), //key, object1, object2, object3
(install("key2", "2000" ,"600", "Test2")),
(install("key3", "3000" ,"120", "Test3")),
(install("key4", "4000" ,"120", "Test4"))
;


Another idea would be:



/* key points to this struct */
struct Value
int i;
int k;
char *c;
;

typedef struct map /* creating a map structure */
struct map *next;
char *KEY;
struct Value value; /* place multiple values inside a struct*/
;


and here is where I am getting stuck:



struct map *insert(char *KEY, struct *Value) 
struct map *np;
unsigned hashval;

if ((np = lookup(KEY)) == NULL)
else
free((struct Value*)np->value); //type cast cannot convert from 'Value' to 'Value*'


if ((np->Value = strdup(Value)) == NULL) //map has no field value
return NULL;


return np;



I searched through the following questions, however was unable to extract the relevant information on how to implement this in C.



Hashmaps having multiple keys with multiple values



How can I assign multiple values to a hash key?



HashMap with multiple valued keys



How can I implement a hashmap which accepts multiple values assigned to the same key?




EDIT



As pointed out in the comments, the structure I am using may not be actuallly a hashmap but a linked list. However the book specifically says that page 144 is a hashmap.










share|improve this question















As a novice programmer I started reading through The C Programming Language to learn more about pointers and structs.



I am currently learning about hashmaps in C. Following the example in the book, I created my own hashmap to hold key-value pairs:



#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_STRING_SIZE 100
#define HASHSIZE 101

static struct map *hashtab[HASHSIZE]; /* pointer table */
unsigned hash(char *); /* hashing function: form hash value for string s. used by both lookup and install*/
char *strdup(char *);

struct map /* creating a map structure */
struct map *next;
char *KEY; /* KEY - pointer to a char - member of nlist*/
char *object1; /* object - pointer to a char - member of nlist*/
;

/* lookup function takes a pointer to char - s as an argument and returns a pointer to map structure */
struct map *lookup(char *s)
struct map *np;

for (np = hashtab[hash(s)]; np != NULL; np = np->next)
if (strcmp(s, np->KEY) == 0)
return np;


return NULL;


/* install function takes a pointer to a char - KEY, object and returns a pointer to map structure */
struct map *install(char *KEY, char *object1)
/* install: put (name, defn) in */
/* Install uses lookup to determine whether the KEY being installed
is already present. Proceeds to create a new entry or update*/
struct map *np;
unsigned hashval;

if ((np = lookup(KEY)) == NULL)
else
free((void *)np->object1);

if ((np->object1 = strdup(object1)) == NULL)
return NULL;

return np;



I then assign values to keys as follows:



int main(void) 

struct map *table[4] =
(install("key1", "value1")),
(install("key2", "value2")),
(install("key3", "value3")),
(install("key4", "value4"))
;

int i;

for (i = 0; i < 4; i++)
printf("%s->%sn", table[i]->KEY, table[i]->object);

printf("n");
return 0;



The code above works well, I am able to assign value1, ... , value4 to key1, ... ,key4 respectively. However, this does not allow me to assign multiple values to the same key.



Let's say I read the following from a text file:



key1 9000 600 Test1
key2 2000 600 Test2
key3 3000 120 Test3
key4 4000 120 Test4
.
.
key10 1000 560 Test10


I want to be able to store each key and assign multiple values to it. Since the number of columns is fixed, maybe I could have a structure that represents a row and put that into the map.



To do so, install needs to be modified to be able to add multiple values for the same key. Since the keys are unique, lookup and uninstall (function I created to remove keys) should remain unchanged.



The code above works perfectly, however I am looking for a general solution to add multiple values to the same keys.



How should I proceed to be able to call:



struct map *table[4] = 
(install("key1", "9000" ,"600", "Test1")), //key, object1, object2, object3
(install("key2", "2000" ,"600", "Test2")),
(install("key3", "3000" ,"120", "Test3")),
(install("key4", "4000" ,"120", "Test4"))
;


Another idea would be:



/* key points to this struct */
struct Value
int i;
int k;
char *c;
;

typedef struct map /* creating a map structure */
struct map *next;
char *KEY;
struct Value value; /* place multiple values inside a struct*/
;


and here is where I am getting stuck:



struct map *insert(char *KEY, struct *Value) 
struct map *np;
unsigned hashval;

if ((np = lookup(KEY)) == NULL)
else
free((struct Value*)np->value); //type cast cannot convert from 'Value' to 'Value*'


if ((np->Value = strdup(Value)) == NULL) //map has no field value
return NULL;


return np;



I searched through the following questions, however was unable to extract the relevant information on how to implement this in C.



Hashmaps having multiple keys with multiple values



How can I assign multiple values to a hash key?



HashMap with multiple valued keys



How can I implement a hashmap which accepts multiple values assigned to the same key?




EDIT



As pointed out in the comments, the structure I am using may not be actuallly a hashmap but a linked list. However the book specifically says that page 144 is a hashmap.







c algorithm pointers data-structures linked-list






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 16:40

























asked Nov 11 at 12:14









Rrz0

467518




467518







  • 2




    Hint: when inserting, treat any duplicate keys exactly as you treat collisions.
    – n.m.
    Nov 11 at 14:15










  • Thanks will look into it. Also, why is Implementing a hashmap with multiple values pointing to the same key considered broad? How can I improve my question?
    – Rrz0
    Nov 11 at 14:26










  • Your structure here is not actually a hash nap, it is a linked list. There are a number of ways you could go to accomplish your objective, but the traditional would be: actually implement a hash nap, and then when you have a key collision, implement a linked list of objects under that key.
    – bluejack
    Nov 11 at 14:33










  • Thanks for the comment. I fail to understand why I am expecting key collisions with such a small number of keys as in my example (10). Also why does the book I am referring to call the structure a hasmap? That is what confused me.
    – Rrz0
    Nov 11 at 14:37







  • 2




    An actual hash table or hash map associates each key with exactly one value. How else could one reasonably retrieve objects from such a data structure? But the one value can be of whatever type you choose, and in particular, and can be a type that can contain multiple other objects, such as a linked list (or the head of one).
    – John Bollinger
    Nov 11 at 18:00












  • 2




    Hint: when inserting, treat any duplicate keys exactly as you treat collisions.
    – n.m.
    Nov 11 at 14:15










  • Thanks will look into it. Also, why is Implementing a hashmap with multiple values pointing to the same key considered broad? How can I improve my question?
    – Rrz0
    Nov 11 at 14:26










  • Your structure here is not actually a hash nap, it is a linked list. There are a number of ways you could go to accomplish your objective, but the traditional would be: actually implement a hash nap, and then when you have a key collision, implement a linked list of objects under that key.
    – bluejack
    Nov 11 at 14:33










  • Thanks for the comment. I fail to understand why I am expecting key collisions with such a small number of keys as in my example (10). Also why does the book I am referring to call the structure a hasmap? That is what confused me.
    – Rrz0
    Nov 11 at 14:37







  • 2




    An actual hash table or hash map associates each key with exactly one value. How else could one reasonably retrieve objects from such a data structure? But the one value can be of whatever type you choose, and in particular, and can be a type that can contain multiple other objects, such as a linked list (or the head of one).
    – John Bollinger
    Nov 11 at 18:00







2




2




Hint: when inserting, treat any duplicate keys exactly as you treat collisions.
– n.m.
Nov 11 at 14:15




Hint: when inserting, treat any duplicate keys exactly as you treat collisions.
– n.m.
Nov 11 at 14:15












Thanks will look into it. Also, why is Implementing a hashmap with multiple values pointing to the same key considered broad? How can I improve my question?
– Rrz0
Nov 11 at 14:26




Thanks will look into it. Also, why is Implementing a hashmap with multiple values pointing to the same key considered broad? How can I improve my question?
– Rrz0
Nov 11 at 14:26












Your structure here is not actually a hash nap, it is a linked list. There are a number of ways you could go to accomplish your objective, but the traditional would be: actually implement a hash nap, and then when you have a key collision, implement a linked list of objects under that key.
– bluejack
Nov 11 at 14:33




Your structure here is not actually a hash nap, it is a linked list. There are a number of ways you could go to accomplish your objective, but the traditional would be: actually implement a hash nap, and then when you have a key collision, implement a linked list of objects under that key.
– bluejack
Nov 11 at 14:33












Thanks for the comment. I fail to understand why I am expecting key collisions with such a small number of keys as in my example (10). Also why does the book I am referring to call the structure a hasmap? That is what confused me.
– Rrz0
Nov 11 at 14:37





Thanks for the comment. I fail to understand why I am expecting key collisions with such a small number of keys as in my example (10). Also why does the book I am referring to call the structure a hasmap? That is what confused me.
– Rrz0
Nov 11 at 14:37





2




2




An actual hash table or hash map associates each key with exactly one value. How else could one reasonably retrieve objects from such a data structure? But the one value can be of whatever type you choose, and in particular, and can be a type that can contain multiple other objects, such as a linked list (or the head of one).
– John Bollinger
Nov 11 at 18:00




An actual hash table or hash map associates each key with exactly one value. How else could one reasonably retrieve objects from such a data structure? But the one value can be of whatever type you choose, and in particular, and can be a type that can contain multiple other objects, such as a linked list (or the head of one).
– John Bollinger
Nov 11 at 18:00












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










This may not be the best possible solution, but thanks to the above comments I got something working. I am declaring multiple values as pointers to char. Please let me know if I can improve this.



I am yet to implement collision correction.



struct map /* creating a map structure */
struct map *next;
char *KEY;
char *value1;
char *value2;
char *value3;
;


I then pass these to the insert function.



struct map *insert(char *KEY, char *value1, char *value2, char *value3) 
/* install: put (name, defn) in */
/* Install uses lookup to determine whether the KEY being installed
is already present. Proceeds to create a new entry or update*/
struct map *np;
unsigned hashval;

if ((np = lookup(KEY)) == NULL)
else
free((void *)np->value1); //type cast cannot convert from 'Value' to 'Value*'
free((void *)np->value2);
free((void *)np->value3);

if ((np->time = strdup(value1)) == NULL) //map has no field value
return NULL;

if ((np->description = strdup(value2)) == NULL) //map has no field value
return NULL;

if ((np->duration = strdup(value3)) == NULL) //map has no field value
return NULL;


return np;



Printing these will give the key pointing the the four values.



key1->value1 value2 value3






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248647%2fc-how-to-assign-multiple-values-to-the-same-key-in-a-hashmap%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    This may not be the best possible solution, but thanks to the above comments I got something working. I am declaring multiple values as pointers to char. Please let me know if I can improve this.



    I am yet to implement collision correction.



    struct map /* creating a map structure */
    struct map *next;
    char *KEY;
    char *value1;
    char *value2;
    char *value3;
    ;


    I then pass these to the insert function.



    struct map *insert(char *KEY, char *value1, char *value2, char *value3) 
    /* install: put (name, defn) in */
    /* Install uses lookup to determine whether the KEY being installed
    is already present. Proceeds to create a new entry or update*/
    struct map *np;
    unsigned hashval;

    if ((np = lookup(KEY)) == NULL)
    else
    free((void *)np->value1); //type cast cannot convert from 'Value' to 'Value*'
    free((void *)np->value2);
    free((void *)np->value3);

    if ((np->time = strdup(value1)) == NULL) //map has no field value
    return NULL;

    if ((np->description = strdup(value2)) == NULL) //map has no field value
    return NULL;

    if ((np->duration = strdup(value3)) == NULL) //map has no field value
    return NULL;


    return np;



    Printing these will give the key pointing the the four values.



    key1->value1 value2 value3






    share|improve this answer


























      up vote
      0
      down vote



      accepted










      This may not be the best possible solution, but thanks to the above comments I got something working. I am declaring multiple values as pointers to char. Please let me know if I can improve this.



      I am yet to implement collision correction.



      struct map /* creating a map structure */
      struct map *next;
      char *KEY;
      char *value1;
      char *value2;
      char *value3;
      ;


      I then pass these to the insert function.



      struct map *insert(char *KEY, char *value1, char *value2, char *value3) 
      /* install: put (name, defn) in */
      /* Install uses lookup to determine whether the KEY being installed
      is already present. Proceeds to create a new entry or update*/
      struct map *np;
      unsigned hashval;

      if ((np = lookup(KEY)) == NULL)
      else
      free((void *)np->value1); //type cast cannot convert from 'Value' to 'Value*'
      free((void *)np->value2);
      free((void *)np->value3);

      if ((np->time = strdup(value1)) == NULL) //map has no field value
      return NULL;

      if ((np->description = strdup(value2)) == NULL) //map has no field value
      return NULL;

      if ((np->duration = strdup(value3)) == NULL) //map has no field value
      return NULL;


      return np;



      Printing these will give the key pointing the the four values.



      key1->value1 value2 value3






      share|improve this answer
























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        This may not be the best possible solution, but thanks to the above comments I got something working. I am declaring multiple values as pointers to char. Please let me know if I can improve this.



        I am yet to implement collision correction.



        struct map /* creating a map structure */
        struct map *next;
        char *KEY;
        char *value1;
        char *value2;
        char *value3;
        ;


        I then pass these to the insert function.



        struct map *insert(char *KEY, char *value1, char *value2, char *value3) 
        /* install: put (name, defn) in */
        /* Install uses lookup to determine whether the KEY being installed
        is already present. Proceeds to create a new entry or update*/
        struct map *np;
        unsigned hashval;

        if ((np = lookup(KEY)) == NULL)
        else
        free((void *)np->value1); //type cast cannot convert from 'Value' to 'Value*'
        free((void *)np->value2);
        free((void *)np->value3);

        if ((np->time = strdup(value1)) == NULL) //map has no field value
        return NULL;

        if ((np->description = strdup(value2)) == NULL) //map has no field value
        return NULL;

        if ((np->duration = strdup(value3)) == NULL) //map has no field value
        return NULL;


        return np;



        Printing these will give the key pointing the the four values.



        key1->value1 value2 value3






        share|improve this answer














        This may not be the best possible solution, but thanks to the above comments I got something working. I am declaring multiple values as pointers to char. Please let me know if I can improve this.



        I am yet to implement collision correction.



        struct map /* creating a map structure */
        struct map *next;
        char *KEY;
        char *value1;
        char *value2;
        char *value3;
        ;


        I then pass these to the insert function.



        struct map *insert(char *KEY, char *value1, char *value2, char *value3) 
        /* install: put (name, defn) in */
        /* Install uses lookup to determine whether the KEY being installed
        is already present. Proceeds to create a new entry or update*/
        struct map *np;
        unsigned hashval;

        if ((np = lookup(KEY)) == NULL)
        else
        free((void *)np->value1); //type cast cannot convert from 'Value' to 'Value*'
        free((void *)np->value2);
        free((void *)np->value3);

        if ((np->time = strdup(value1)) == NULL) //map has no field value
        return NULL;

        if ((np->description = strdup(value2)) == NULL) //map has no field value
        return NULL;

        if ((np->duration = strdup(value3)) == NULL) //map has no field value
        return NULL;


        return np;



        Printing these will give the key pointing the the four values.



        key1->value1 value2 value3







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 11 at 17:53

























        answered Nov 11 at 17:29









        Rrz0

        467518




        467518



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248647%2fc-how-to-assign-multiple-values-to-the-same-key-in-a-hashmap%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Top Tejano songwriter Luis Silva dead of heart attack at 64

            ReactJS Fetched API data displays live - need Data displayed static

            政党