Is it possible to write a macro that has a type and a value as its input parameters (MACRO(type,value)), and returns a valid pointer to a location that holds the submitted value.
This macro should perform like the following function, but in a more generic manner:
int *val_to_ptr(int val){
int *r = NULL;
r = nm_malloc(sizeof(*r));
*r = val;
return r;
}
Where nm_malloc() is a failsafe malloc.
The Macro usage should be compatible with this usage:
printf("%d",*MACRO(int,5));
Is it possible to achieve that ?
Here’s an implementation which is modelled on c++ templates
You’d have to explicitly define what types you’re using it for, and would have to typedef things like
unsigned intordouble complexor the token paste won’t work.Alternately use
and call
without having to worry about argument evaluation