Question

How do you implement function Key *key_construc(char *in_name, int in_id);? Hint: first use malloc(sizeof(Key)), next use...

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()).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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
}

Add a comment
Know the answer?
Add Answer to:
How do you implement function Key *key_construc(char *in_name, int in_id);? Hint: first use malloc(sizeof(Key)), next use...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT