int: printInt, \ float: printFloat, \ char*: printString \)(x) void printInt(int x) { printf(“%d\n”, x); } void printFloat(float x) { printf(“%f\n”, x); } void printString(char* x) { printf(“%s\n”, x); } ```
- Code Example 2: Using `void*` Pointers for Generic Data Types
switch (type) {
case 'i': printf("%d\n", *(int*)value); break;
case 'f': printf("%f\n", *(float*)value); break;
// Add more cases as needed
}
}
```
- Code Example 3: Generic Stack Using `void*`
void** items; int top; int maxSize;} GenericStack; void push(GenericStack* s, void* item) { /* Implementation */ } void* pop(GenericStack* s) { /* Implementation */ } ```
- Code Example 4: Function Pointers for Generic Behavior