+1  A: 

That should work. To create copy_key_sint, use copy_key_ ## sint.

If you can't get this to work with CPP, then write a small C program which generates a C source file.

Aaron Digulla
+1  A: 

Try something like this (I just tested the compilation, but not the result in an executed program):

 #include "memory.h"

   #define COPY_KEY(type, name)                                 \
   type name(void *key, void **args, int argc, void **out) { \
      if (*out = malloc(sizeof(type))) {                     \
         return 1;                                           \
      }                                                      \
   **((type **) out) = *((type *) key);                      \
   return 0;                                                 \
   }                                                         \

COPY_KEY(int, copy_key_sint)

For more on the subject of generic programming in C, read this blog wich contains a few examples and also this book which contains interesting solutions to the problem for basic data structures and algorithm.

Matthieu
A: 

Wouldn't a macro which just takes sizeof(*key) and calls a single function that uses memcpy be a lot cleaner (less preprocessor abuse and code bloat) than making a new function for each type just so it can do a native assignment rather than memcpy?

My view is that the whole problem is your attempt to apply C++ thinking to C. C has memcpy for a very good reason.

R..
@R.: I don't think this is answer to the question of the OP. I understand that code that he is giving as just one type of example of what kind of *declarations* he wants to achieve.
Jens Gustedt
I think my answer applies to a large category of situations for which one might want "C++ templates for C". Often if you realize you can treat the data as an abstract byte array, or partly as such with a callback function for processing it (as in `qsort`, etc.), you're able to write much simpler and more efficient C code.
R..
Does memcpy have a Windows implementation?
gamecoder
@gamecoder: Huh? `memcpy` is part of the C language.
R..
+2  A: 

I don't understand the example function that you are giving very well, but using macros for the task is relatively easy. Just you wouldn't give strings to the macro as arguments but tokens:

#define DECLARE_MY_COPY_FUNCTION(TYPE, SUFFIX)              \
int copy_function_ ## SUFFIX(unsigned count, TYPE* arg)

#define DEFINE_MY_COPY_FUNCTION(TYPE, SUFFIX)               \
int copy_function_ ## SUFFIX(unsigned count, TYPE* arg) {   \
 /* do something with TYPE */                               \
 return whatever;                                           \
}

You may then use this to declare the functions in a header file

DECLARE_MY_COPY_FUNCTION(unsigned, toto);
DECLARE_MY_COPY_FUNCTION(double, hui);

and define them in a .c file:

DEFINE_MY_COPY_FUNCTION(unsigned, toto);
DEFINE_MY_COPY_FUNCTION(double, hui);

In this version as stated here you might get warnings on superfluous `;'. But you can get rid of them by adding dummy declarations in the macros like this

#define DEFINE_MY_COPY_FUNCTION(TYPE, SUFFIX)               \
int copy_function_ ## SUFFIX(unsigned count, TYPE* arg) {   \
 /* do something with TYPE */                               \
 return whatever;                                           \
}                                                           \
enum { dummy_enum_for_copy_function_ ## SUFFIX }
Jens Gustedt