Advertisement
STANAANDREY

c generic swap

Jan 12th, 2023
754
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.36 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. void swap(void *a, void *b, size_t width)
  6. {
  7.     void *temp = malloc(width);
  8.     memcpy(temp, b, width);
  9.     memcpy(b, a, width);
  10.     memcpy(a, temp, width);
  11.     free(temp);
  12. }
  13.  
  14. int main() {
  15.     char a = 'a', b = 'b';
  16.     swap(&a, &b, sizeof(a));
  17.     printf("%c %c\n", a, b);
  18.     return 0;
  19. }
  20.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement