From: Bastian Dehn Date: Sun, 1 Oct 2017 20:38:38 +0000 (+0200) Subject: change: array with realloc X-Git-Url: https://gitweb.hhaalo.de/?a=commitdiff_plain;h=c3a4a8c;p=discspan.git change: array with realloc --- diff --git a/discspan.c b/discspan.c index fe3429c..3295ff4 100644 --- a/discspan.c +++ b/discspan.c @@ -101,16 +101,16 @@ void splitter(const char *input, printHumanReadSize(output, splitgroesse); } -int cmpfunc(const void * filea, - const void * fileb) { - long long unsigned int a = ((const struct file_size*)filea)->fsize; - long long unsigned int b = ((const struct file_size*)fileb)->fsize; +int cmpfunc(const void *filea, + const void *fileb) { + long long unsigned int a = ((const struct file_size*) filea)->fsize; + long long unsigned int b = ((const struct file_size*) fileb)->fsize; if (a < b) { return 1; - } else if(a > b) { + } else if (a > b) { return -1; - } else if(a == b) { + } else if (a == b) { return 0; } else { return -2; @@ -137,7 +137,8 @@ void filesize_sort(const char *input, fseek(in, 0L, SEEK_SET); // struct Array with Dateienanzahl - struct file_size fs[lines -1]; + struct file_size *fs = NULL; + fs = (struct file_size *) malloc((lines -1) * sizeof(struct file_size)); lines = 0; while (fscanf(in, "%[^\n]\n", pfad) == 1) { @@ -160,6 +161,8 @@ void filesize_sort(const char *input, } } + fs = (struct file_size *) realloc(fs, (lines+1) * sizeof(struct file_size)); + // sortiere das array fs mit stucts qsort(fs, lines, sizeof(struct file_size), cmpfunc);