/** * @file funcpointer.cpp * @brief Demonstrates function pointers in C++ * * Key Learning Points: * 1. Function pointers allow us to store and call functions dynamically * 2. Same interface can call different implementations based on data type * 3. Eliminates need for if-else statements to determine which function to call * 4. Makes code more flexible and extensible */ #include #include // Define two different data types typedef struct Student { char name[50]; } Student; typedef struct Employee { int id; } Employee; // Print functions - these will be stored in function pointers void printStudent(void *data) { Student *s = (Student *)data; printf("Student Name: %s", s->name); } void printEmployee(void *data) { Employee *e = (Employee *)data; printf("Employee ID: %d", e->id); } // Define a node which can hold any type of data and a function pointer to print it typedef struct node { void *data; struct node *next; void (*print)(void *); // Function pointer - this is the key concept! } Node; // Add a new node to the linked list Node *addNode(Node *head, void *data, void (*printFunction)(void *)) { Node *newNode = (Node *)malloc(sizeof(Node)); newNode->print = printFunction; newNode->data = data; newNode->next = NULL; if (!head) { return newNode; } Node *temp = head; while (temp->next) { temp = temp->next; } temp->next = newNode; return head; } int main() { Node *head = NULL; Student s1 = {"Alice"}; head = addNode(head, &s1, printStudent); Employee e1 = {25}; head = addNode(head, &e1, printEmployee); // Print the data using function pointers - no type checking needed! Node *temp = head; while (temp) { temp->print(temp->data); printf("\n"); temp = temp->next; } // Clean up while (head) { Node *toDelete = head; head = head->next; free(toDelete); } return 0; } /** The loop starting on line 98 is a print loop that uses function pointers. It iterates through a linked list of nodes, printing the data contained in each node without explicitly checking the type of the data. Here's a breakdown of how the loop works: The loop starts with the condition while(temp), which means it will continue as long as the temp pointer is not null. This ensures that the loop iterates through all the nodes in the linked list. Inside the loop, temp->print(temp->data) is called. Here, temp->print is a function pointer that points to a print function specific to the type of data stored in the node. By using function pointers, the appropriate print function is called based on the type of data without the need for an explicit if-else check. After printing the data, printf("\n") is called to add a newline character, which creates a line break between each printed data. Finally, temp = temp->next is executed to move the temp pointer to the next node in the linked list, allowing the loop to continue until all nodes have been processed. Overall, this loop simplifies the printing process by utilizing function pointers, eliminating the need for an if-else statement to determine the type of data and the corresponding print function to use. */