Advertisement
STANAANDREY

vasort

Mar 30th, 2023 (edited)
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.68 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdarg.h>
  4.  
  5. int cmp(const void *p1, const void *p2) {
  6.   return *(int*)p1 - *(int*)p2;
  7. }
  8.  
  9. void vaSort(int n, ...) {
  10.   va_list va;
  11.   va_start(va, n);
  12.  
  13.   int *arr = calloc(sizeof(int), n);
  14.   if (arr == NULL) {
  15.     perror("");
  16.     exit(EXIT_FAILURE);
  17.   }
  18.   for (int i = 0; i < n; i++) {
  19.     arr[i] = *va_arg(va, int*);
  20.   }
  21.   qsort(arr, n, sizeof(*arr), cmp);
  22.  
  23.   va_start(va, n);
  24.   for (int i = 0; i < n; i++) {
  25.     *va_arg(va, int*) = arr[i];
  26.   }
  27.  
  28.   free(arr);
  29.   va_end(va);
  30. }
  31.  
  32. int main(int argc, char *argv[]) {
  33.   int a = 2, b = 3, c = 1;
  34.   vaSort(3, &a, &b, &c);
  35.   printf("%d %d %d\n", a, b, c);
  36.   return 0;
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement