I need to implement some methods that do stuff with different kinds of number arrays. Usually, I\'d use generics for that job, but as C doesn\'t provide them, I\'m now tryin
Actually, the best you can do is to define a macro that will generate the function for the given type.
#define define_get_minimum(T) \
T get_minimum_##T(T* nums, int len){ \
T min = nums[0]; \
for (int i = 1; i < len; i++) { \
if (nums[i] < min) { \
min = nums[i]; \
} \
} \
return min; \
}
Then, you can call that macro to define the specializations you need (with C++ template, a similar thing is done automagically by the compiler).
define_get_minimum(int)
define_get_minimum(double)
define_get_minimum(float)
Another thing that a C++ compiler does automagically is deduce the overloaded function you need. You can't have that in C, so you will have to tell you are using the it specialization. You can simulate a template-like syntax for your function with the following macro (the C++ <> are just replaced by ()):
#define get_minimum(T) get_minimum_##T
Then, you should be able to call it the following way:
int main()
{
// Define arr as char* array...
// Do stuff...
int res = get_minimum(int)(arr, 3);
}
I did not test this code, but it should work.