How can I pass a “Type” as a argument to function in c?
Here's an example for for some macro trickery.
func.h
#ifndef FUNC_H
#define FUNC_H
#define add(a, b, typename) functionAdd##typename(a,b)
/* function declarations */
#define declared(typename) \
typename functionAdd##typename(typename, typename)
declared(int);
declared(float);
#endif
func.c
#include "func.h"
/* function code */
#define functionAdd(a, b, typename) \
typename functionAdd##typename(typename a, typename b){ \
return a+b; \
}
/* function bodies (definitions) */
functionAdd(a, b, int)
functionAdd(a, b, float)
main.c
#include <stdio.h>
#include "func.h"
int main()
{
int x1 = add(1, 2, int);
float x2 = add(3.0, 4.0, float);
printf("%d %f\n", x1, x2);
return 0;
}
This is only possible if you have a standard C compiler, in which case you can use the _Generic
keyword for this purpose. You have to write a different function per supported type.
#include <stdio.h>
#define func(x) _Generic((x), int: func_int, char: func_char)(x);
void func_int (int x)
{
printf("%s\t%d\n", __func__, x);
}
void func_char (char x)
{
printf("%s\t%c\n", __func__, x);
}
int main(void)
{
int i = 5;
char c = 'A';
func(i);
func(c);
}
Output:
func_int 5
func_char A
You don't pass the "type". C has no built-in way to encode and decode type information at runtime. The function operating on the objects must know the type statically. If you are absolutely intent on working with pointers to void
, you have to delegate to a function that knows the type information. That can be done with a callback. For instance, the standard library function qsort
accepts a callback for comparing the objects' values:
void qsort( void *ptr, size_t count, size_t size,
int (*comp)(const void *, const void *) );
The calling code supplies the callback, and inside said callback it will cast back to the static type it needs to compare. That's how one usually works with pointers to void
, one defines the set of operations it needs to do on the type in an abstract form, and then asks the calling code to supply an implementation for those operations.