From: Jerome Benoit Date: Thu, 23 Mar 2017 15:26:28 +0000 (+0100) Subject: TP 13 exo1: Implement a FIXME on array dynamic resizing X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=ac5c12a4131a461182da110ec6c50be4cbd4270e;p=TD_C.git TP 13 exo1: Implement a FIXME on array dynamic resizing Signed-off-by: Jerome Benoit --- diff --git a/TP_13/exo1/lib/array.c b/TP_13/exo1/lib/array.c index db8ab7e..c4e227d 100644 --- a/TP_13/exo1/lib/array.c +++ b/TP_13/exo1/lib/array.c @@ -43,6 +43,7 @@ int* concat_tab(int tab1[], unsigned tab_size1, int tab2[], unsigned tab_size2) int* resize_tab(int tab[], unsigned old_tab_size, unsigned new_tab_size) { tab = realloc(tab, sizeof(int) * new_tab_size); + /* zero by default the added cells */ if (old_tab_size < new_tab_size) { for (unsigned i = old_tab_size; i < new_tab_size; i++) { tab[i] = 0; diff --git a/TP_13/exo1/lib/io.c b/TP_13/exo1/lib/io.c index 69b13d7..b0a66a0 100644 --- a/TP_13/exo1/lib/io.c +++ b/TP_13/exo1/lib/io.c @@ -15,10 +15,10 @@ int* prompt_array(int array[], unsigned* size) { int errno = prompt_value("Taille du tableau?", (int*)size); array = create_tab(array, *size); for (unsigned i = 0; i < *size; i++) { - errno += prompt_value("Valeur?", &array[i]); + errno += prompt_value("Valeur?", &array[i]); + /* error might have occured */ + handle_prompt_error(errno); } - /* error might have occured */ - handle_prompt_error(errno); return array; } @@ -102,9 +102,14 @@ void do_resize(int array[], unsigned* old_size) { errno = prompt_value("\nNouvelle taille?", (int*)&new_size); handle_prompt_error(errno); - /* FIXME: one should able the set the array new content if new_size > *old_size - * for now, new values are zeroed */ array = resize_tab(array, *old_size, new_size); + if (new_size > *old_size) { + printf("Saisie des valeurs supplementaires du tableau\n"); + for (unsigned i = *old_size; i < new_size; i++) { + errno += prompt_value("Valeur?", &array[i]); + handle_prompt_error(errno); + } + } *old_size = new_size; }