Pass the field name of struct to access inside a function
I have a linked list and I made a function to fetch a node. But I want to use it both to search by first or last name. typedef struct people { char name[60], lastname[60]; struct people *next; } people; people *search(const char *key, people *list, FIELD) { while (list && strcmp(key, list->FIELD) != 0) { list = list->next; } return list; } Example: people *aux; aux = search("John", list_of_people, "name"); Or: aux = search("Smith", list_of_people, "lastname"); There is a clear and efficient way to solve this problem without repeating code? use offsetof(<stddef.h>) macro. E.g: people *search