#include <stdio.h>
#include <stdlib.h>
int compare(const void *a, const void *b);
int main(void) {
int input;
scanf("%d", &input);
int* array = (int *) calloc(input, sizeof(int));
for (int i = 0; i < input; i++) {
scanf("%d", &array[i]);
}
qsort(array, sizeof(array)/sizeof(int), sizeof(int), compare);
for (int i = 0; i < input; i++) printf("%d ", array[i]);
return 0;
}
int compare(const void *a, const void *b)
{
int num1 = *(int *)a;
int num2 = *(int *)b;
if (num1 < num2) return -1;
if (num1 > num2) return 1;
return 0;
}
I am still a student of C language basics. It might be a very basic question because you haven't learned it all right. I'm trying to sort a dynamic array. I created and sorted a dynamic array, but looking at the result, there is no sorting at all. What is the problem?
arrayis a pointer. Sosizeof(array)is a size of the pointer.int inputcontains the number of elements to sort.