From: Jérôme Benoit Date: Mon, 13 Mar 2017 14:00:07 +0000 (+0100) Subject: TP 11 exo1: simplify the logic in the array creation and resizing X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=34dd19e9551e120279afde8d75a1cb51181d7794;p=TD_C.git TP 11 exo1: simplify the logic in the array creation and resizing Signed-off-by: Jérôme Benoit --- diff --git a/TP_13/exo1/lib/array.c b/TP_13/exo1/lib/array.c index f7e324f..d2e6182 100644 --- a/TP_13/exo1/lib/array.c +++ b/TP_13/exo1/lib/array.c @@ -4,15 +4,13 @@ int* create_tab(int tab[], unsigned tab_size) { tab = malloc(sizeof(int) * tab_size); - if (tab == NULL) { - return NULL; - } else { + if (tab != NULL) { /* initialize to zero the integer array */ for (unsigned i = 0; i < tab_size; i++) { tab[i] = 0; } - return tab; } + return tab; } void free_tab(int tab[]) { @@ -38,11 +36,7 @@ int* concat_tab(int tab1[], unsigned tab_size1, int tab2[], unsigned tab_size2, int* resize_tab(int tab[], unsigned new_tab_size) { tab = realloc(tab, sizeof(int) * new_tab_size); - if (tab == NULL) { - return NULL; - } else { - return tab; - } + return tab; } /* number of occurences of an element in an unsorted array */ diff --git a/TP_13/exo1/src/main.c b/TP_13/exo1/src/main.c index 658930e..9f23749 100644 --- a/TP_13/exo1/src/main.c +++ b/TP_13/exo1/src/main.c @@ -17,6 +17,8 @@ int main() { const unsigned tab_new_size = 20; tab = resize_tab(tab, tab_new_size); + printf("%d\n", tab_new_size); + display_array(tab, tab_new_size); free_tab(tab);