How do you implement function Key *key_construc(char *in_name, int in_id);?
Hint: first use malloc(sizeof(Key)), next use malloc( (strlen(in name)+1)*sizeof(char) ), then use strcpy(). (Do not use strdup()).
You have not given the struct defintion of Key. I presume it has name and id fields in it. If not, post a comment and I'll help to fix the code.
Key *key_construc(char *in_name, int in_id)
{
Key* k = (Key*)malloc(sizeof(Key)); //allocate memory for the
struct
k->name = (char*) malloc((strlen(in_name)+1)*sizeof(char));
//allocate memory for the name string since its pointer
strcpy(k->name, in_name); //copy the name into the struct's name
field
k->id = in_id; //set the id in the struct
return k; //return the allocated key
}
How do you implement function Key *key_construc(char *in_name, int in_id);? Hint: first use malloc(sizeof(Key)), next use...