/* ECP: FILEname=fig7_26.c */ /* 1*/ /* Read An Unlimited Number Of Ints */ /* 2*/ /* With No Attempts At Error Recovery */ /* 3*/ /* Correct Implementation */ /* 4*/ /* Returns A Pointer To The Data */ /* 5*/ /* ItemsRead Is Set By Reference To #Items Read */ /* 6*/ int * /* 7*/ GetInts( int * const ItemsRead ) /* 8*/ { /* 9*/ int NumRead = 0; /*10*/ int ArraySize = 5; /*11*/ int InputVal, *Array; /*12*/ /*13*/ Array = malloc( sizeof( int ) * ArraySize ); /*14*/ if( Array == NULL ) /*15*/ Error( "Out of memory" ); /*16*/ printf( "Enter any number of integers: " ); /*17*/ while( scanf( "%d", &InputVal ) == 1 ) /*18*/ { /*19*/ if( NumRead == ArraySize ) /*20*/ { /* Array Doubling Code */ /*21*/ ArraySize *= 2; /*22*/ Array = realloc( Array, sizeof( int ) * ArraySize ); /*23*/ if( Array == NULL ) /*24*/ Error( "Out of memory" ); /*25*/ } /*26*/ Array[ NumRead++ ] = InputVal; /*27*/ } /*28*/ *ItemsRead = NumRead; /*29*/ return realloc( Array, sizeof( int ) * NumRead ); /*30*/ } /* 1*/ main( void ) /* 2*/ { /* 3*/ int *NumArray; /* 4*/ int ItemsRead; /* 5*/ NumArray = GetInts( &ItemsRead ); /* 6*/ PrintNondup( NumArray, ItemsRead ); /* 7*/ return !ItemsRead; /* 8*/ }