Reputation: 13
How I can create function with other types of data (some struct or sth)? In C++ exist templates, but in C?
I hear about void *, but i dont know if it works.
Any ideas?
Upvotes: 0
Views: 2795
Reputation: 10717
Well, the way to do it is with void *
. You might also need use function pointers, for example if you need to compare generic values.
The other way to do it is to use xmacros, but that's generally more for reducing code duplication for very similar structures.
Upvotes: 2
Reputation: 2026
void *
is the solution in C as any pointer has the same sizeof()
as void *
. Of course, you get no type safety, but it's as good an abstraction as you can get with C. Furthermore, you could look at stdarg.h
and variadic functions, but again, you should keep track yourself of what you're doing, since the compiler won't aid you one bit.
Upvotes: -1