Here are some exercises for toning your C muscles. NOTE: In the below text:
#define IF_WRONG_CORRECT_IT "Is there something wrong with this code? How would you correct it?"
EASY: Recursive structs: Consider the code fragment:
struct list_node { Person data; struct list_node *next; };Can we have a struct list_node next inside struct list_node (instead of a pointer to structure)?
EASY: Consider the code fragment:
struct Person { char *name; int age; Vehicle myVehicle; }; struct Vehicle { char *numPlate; char* model; Person owner; };What is wrong with the above code?
MEDIUM: Comment on the efficiency of the below function ’printPerson’. How can you improve? Assume that you cannot change the character arrays to char pointers.
struct Address{ char street[128]; char area[128]; char city[128]; char country[64]; }; struct Person { char name[128]; struct Address address; }; void printPerson(struct Person person) { // Print the name and the address of the person }
struct point { int x, y; }; struct rect { struct point leftBottom, rightTop; }; struct rect r, &rectPtr = &r;(NOTE: Answer on page number 131 in K&R. Dont refer to the book until you try)
An excellent set of exercise problems can be found at Stanford linked list problems. If you can, attempt all the problems there. Its an excellent guide to brush up and improve your linked list skills. If you cannot offord time to solve everything, here is a basic list of must-do problems.