return_type (*pointer_name)(parameter_types);
void foo() { /*...*/ }
void bar() { /*...*/ }
void (*func_ptr)();
if (condition) {
func_ptr = foo;
} else {
func_ptr = bar;
}
func_ptr(); // Calls either foo or bar based on the condition
Student: Contains a name field and a static print method.Employee: Contains an id field and a static print method.Node: Holds a generic data pointer, a print function pointer, and a next pointer for the linked list.Student Structtypedef struct Student {
char name[50];
static void print(void *node) {
Student *s = (Student *)node;
printf("Student:\n");
printf("Name: %s\n", s->name);
}
} Student;
Employee Structtypedef struct Employee {
int id;
static void print(void *node) {
Employee *e = (Employee *)node;
printf("Employee:\n");
printf("Employee ID: %d\n", e->id);
}
} Employee;
Node Structtypedef struct node {
void *data;
struct node *next;
int type;
void (*print)(void *);
} Node;
addNode(Node *head, void *data, void (*printFunction)(void *), int type)Usage:
Student s1 = {"Alice"};
head = addNode(head, &s1, Student::print, STUDENT);
Employee e1 = {25};
head = addNode(head, &e1, Employee::print, EMPLOYEE);
+----------------+ +----------------+
| Node 1 | | Node 2 |
| (Student) | | (Employee) |
+----------------+ +----------------+
| data | ---> | data | --->
| next | --- | next | ---> NULL
| type: STUDENT | | type: EMPLOYEE |
| print | | print |
+----------------+ +----------------+
Details:
Node 1:
- data points to a Student object:
+-----------------+
| Student |
|-----------------|
| name: "Alice" |
+-----------------+
- next points to Node 2
- type: STUDENT (0)
- print points to Student::print function
Node 2:
- data points to an Employee object:
+-----------------+
| Employee |
|-----------------|
| id: 25 |
+-----------------+
- next is NULL (end of list)
- type: EMPLOYEE (1)
- print points to Employee::print function
Node *temp = head;
while (temp) {
temp->print(temp->data);
printf("\n");
temp = temp->next;
}
temp->print: Function pointer to the appropriate print function (Student::print or Employee::print).temp->data: Pointer to the data (Student or Employee object).print function without checking the type, leveraging function pointers for dynamic dispatch.jal (Jump and Link) with a label or address known at compile time.jalr (Jump and Link Register) with the function’s address stored in a register.a0 to a7: Argument and return value registers.ra: Return address register.s0: Frame pointer (used to access local variables on the stack)..LBB1_8:
lw a0, -76(s0) # Load 'temp' from the stack into 'a0'
lw a1, 12(a0) # Load 'temp->print' into 'a1'
lw a0, 0(a0) # Load 'temp->data' into 'a0'
jalr a1 # Call the function pointed by 'a1' with 'a0' as argument
Load the temp Pointer
lw a0, -76(s0)
temp from the (offset -76 from s0) into register a0. s0 is base address of temp objectLoad the Function Pointer (temp->print)
lw a1, 12(a0)
12 within the Node struct into register a1.
Node struct layout:
data at offset 0next at offset 4 (assuming 32-bit pointers)type at offset 8print at offset 12Load the Data Pointer (temp->data)
lw a0, 0(a0)
data pointer from offset 0 within the Node struct into register a0.Call the Function via Function Pointer
jalr a1
a1 (the function pointer) and link (store return address in ra).C++ Code:
temp->print(temp->data);
Assembly Translation:
temp: lw a0, -76(s0)print function pointer: lw a1, 12(a0)data pointer: lw a0, 0(a0)jalr a1Process:
a0.jalr a1 calls the function whose address is in a1.Initialization
temp is set to point to the head of the linked list.Loop Condition Check
temp is not NULL.Function Pointer Invocation
temp into a0:
temp->print into a1:
temp->data into a0:
jalr a1:
Advance to Next Node
temp to temp->next to proceed to the next node in the list.Repeat
temp becomes NULL.a0: temp->data (argument for print function)a1: temp->print (address of print function)jalr a1 jumps to temp->print.ra.jalr a1.