#include <stdio.h>
// Function that returns a pointer to void
void* my_function() {
int* my_int = (int*)malloc(sizeof(int)); // Allocate memory for an integer
if (my_int == NULL) {
perror("malloc failed");
return NULL; // Handle allocation failure
}
*my_int = 42; // Assign a value to the allocated integer
return (void*)my_int; // Cast to void* and return the pointer
}
// Function that takes a pointer to my_function as an argument
void process_data(void (*func_ptr)()) {
void* data_ptr = func_ptr(); // Call the function through the pointer
if (data_ptr != NULL) {
// Now you need to know the *type* of data pointed to by data_ptr
// to use it safely. In this case, we know it's an int*.
int* int_ptr = (int*)data_ptr; // Cast back to the original type
printf("Value: %d\n", *int_ptr);
free(data_ptr); // Important: Free the allocated memory
} else {
printf("Function returned NULL.\n");
}
}
int main() {
// Declare a pointer to a function that takes no arguments and returns a void*
void (*func_ptr)();
// Assign the address of my_function to the function pointer
func_ptr = &my_function; // or simply func_ptr = my_function;
// Call the function through the function pointer
process_data(func_ptr);
return 0;
}
Explanation:
void* my_function():
This function allocates memory using malloc (important to allocate memory if you're returning a pointer to something that needs to exist beyond the function's scope).
It assigns a value to the allocated memory.
It returns a void*. void* is a generic pointer type that can point to any data type. It's often used when you want a function to return a pointer to data without specifying the exact type.
The (void*)my_int cast is necessary because my_int is an int*, and we need to explicitly convert it to a void* for the return type.
void process_data(void (*func_ptr)()):
This function takes a function pointer as an argument.
void (*func_ptr)() declares a function pointer named func_ptr. Let's break it down:
void: The function pointed to returns void.
(*func_ptr): This indicates that func_ptr is a pointer.
(): The function pointed to takes no arguments.
Inside process_data, void* data_ptr = func_ptr(); calls the function pointed to by func_ptr and stores the returned void* in data_ptr.
int main():
void (*func_ptr)(); declares a function pointer.
func_ptr = &my_function; assigns the address of my_function to the function pointer. The & is optional in this case, as the function name decays into a pointer to the function.
process_data(func_ptr); calls process_data, passing the function pointer as an argument.
Important Considerations:
Type Safety: The biggest challenge with void* is that it bypasses type checking. You must know the actual type of data being pointed to by the void* before you can dereference it. In the example, we know that my_function returns a pointer to an int, so we cast data_ptr back to int* before using it. If you cast to the wrong type, you'll get undefined behavior (crashes, incorrect results, etc.).
Memory Management: If the function returns a pointer to dynamically allocated memory (as in this example), the caller (in this case, process_data) is responsible for freeing that memory using free() to prevent memory leaks.
Error Handling: Always check for NULL return values from malloc and other functions that allocate memory. This helps prevent crashes.
Why use a function pointer that returns a void*?
Genericity: It allows you to write functions that can work with different data types without being tied to a specific type.
Abstraction: It hides the details of the data type from the caller.
Flexibility: It allows you to dynamically choose which function to call and what type of data it returns. This is useful in situations like callbacks or plugin architectures.