

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 6
click for more info
Not enough gems
Cost: 6 gems
1: Pointer-Pointers
incomplete
2: Array of Pointers
incomplete
3: Void Pointers
incomplete
4: Swapping Integers
incomplete
5: Swapping Strings
incomplete
6: Generic Swap
incomplete
This lesson's interactive features are locked, please to keep using them
For the previous swap implementations we've known the type of the data we want to swap. Because we knew the type, the compiler knew the sizes of the data we want to swap.
However, to make a generic swap, we will need to provide the C compiler with the size of the data that we are swapping because void * loses that type info. Our new interface for swap will include the size:
void swap(void *vp1, void *vp2, size_t size);
The other problem we're going to have is that directly assigning pointer values does not work the same way with void *. Instead of using *ptr1 = *ptr2, we will use memcpy, which is included in the string.h library:
void *memcpy(void *destination, void* source, size_t size);
So to move the data from ptr2 to ptr1, we will use the following:
memcpy(ptr1, ptr2, size);
Implement the generic swap() function.