Generic Byte Swap with void Pointers and memcpy
hardQuestion 476Write one swap function that works for any type, then use it on three of them.
void gswap(void *a, void *b, size_t size) must exchange the size bytes at a with the size bytes at b, moving them with memcpy through a malloc'd scratch buffer that it frees before returning.
Read from stdin, in this order: two integers x and y; two doubles d1 and d2; five integers into an array; two indices i and j (0-based, always valid).
Swap x with y, d1 with d2, and arr[i] with arr[j] — every swap through gswap, with sizeof giving the size.
Print exactly three lines: "ints: " then the two integers; "doubles: " then the two doubles with exactly 2 decimal places; the five array values. Values on a line are separated by single spaces.
Sample input: 3 7 2.50 9.75 10 20 30 40 50 0 4
Free the memory you allocate before the program ends — this question checks for memory leaks.
Expected output
ints: 7 3 doubles: 9.75 2.50 50 20 30 40 10
= newline. The output ends with a newline, so print one after the last line.
Your output
Run your code to see the output here...