From 34dd19e9551e120279afde8d75a1cb51181d7794 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 13 Mar 2017 15:00:07 +0100 Subject: [PATCH] TP 11 exo1: simplify the logic in the array creation and resizing MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- TP_13/exo1/lib/array.c | 12 +++--------- TP_13/exo1/src/main.c | 2 ++ 2 files changed, 5 insertions(+), 9 deletions(-) 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); -- 2.34.1