1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
int main( int argc, char** argv )
{
/* allocation d un tableau de nb_elem, effectivement nb_elem vient de nulpart, mais c est pas la question */
int* tab = malloc( nb_elem * sizeof( int ) );
/* faudrait tester le retour du malloc, une allocation peut echouée */
int* tmp_tab = 0;
/* je veux plus d elements dans mon tableau, ici 1 de plus */
++nb_elem;
/* pour ca on appel realloc = reallocation !
** mais attention on recupere le pointeur retour dans une varaible temporaire
** car en cas d echec on ne veux pas perdre le tableau original
*/
tmp_tab = realloc( tab, nb_elem * sizeof( int ) );
/* si la reallocation c est bien passe on affecte la valeur du pointeur
** dans tab
*/
if( tmp_tab != 0 )
{
tab = tmp_tab;
/* puis ici faire le reste */
}
return 0;
} |