Unfortunately, in C, that is hard to do, but something like this:
Code:
char *buffer = NULL;
size_t max_size = 0;
void Swap(void *Ptr_A,void *Ptr_B, size_t size)
{
// Allocate a new buffer when we need a bigger one. Since a big buffer can hold small things,
// there's no need to "reduce" the size of the buffer.
if (size > max_size)
{
free(buffer);
buffer = malloc(size);
max_size = size;
}
if (buffer == NULL) // Do some panicing ;
memcpy(buffer, Ptr_A, size);
memcpy(PtrA, Ptr_B, size);
memcpy(Ptr_B, buffer, size);
}
You should of course also free the temporary buffer at some point.
Alternatively, you could use a macro:
Code:
#define SWAP(T, A, B) do { T temp = (A); (A) = (B); (B) = temp; } while(0)
Use like this:
Code:
struct something
{
// stuff here
};
...
struct something a, b;
...
SWAP(struct something, a, b);
--
Mats