I have a function f, defined as following:
struct s {
void *data;
struct s *next;
};
void
f(struct s **p, void *q)
{
/* ... */
}
void *
g(struct s **p)
{
/* ... */
}
I have to test these functions, using a lot of different arguments. But pointers to void can contain only an object address, right ? So how can I automate operations as following, without using temporary variable (or maybe in a macro).
f(p, 2);
f(p, 'c');
f(p, 3.14);
There’s no way around of a temporary variable (well, you can use a static variable, it sure isn’t temporary!) because you need an address, thus you need a (non-register) variable.
However, that doesn’t mean you need additinal lines of code: you can use a typed initializer (as of C99):
However, the function (as you have defined it) has no way of knowing how big an object you have just passed to it, and it must not just store the pointer value because it’s a pointer to a very temporary variable.
(Integers can be converted to pointers and back again with some restrictions but you have to check your implementation for details.)