#include #include /* int sum( int * arr, int N ) { int i; int theSum = 0; for( i = 0; i < N; i++ ) theSum += arr[ i ]; return theSum; } */ int sum( int *arr, int N ) { int *p = arr; int *endMarker = arr + N; int theSum = 0; while( p != endMarker ) theSum += *p++; return theSum; } /* * Fill in *N with # items read * Return arr of items allocated from memory heap */ int * readInts( int *N ) { int capacity = 5; int itemsRead = 0; int *arr = malloc( capacity * sizeof( int ) ); int x; printf( "Enter as many numbers, followed by ***\n" ); while( scanf( "%d", & x ) == 1 ) { if( itemsRead == capacity ) { capacity *= 2; arr = realloc( arr, capacity * sizeof( int ) ); if( arr == NULL ) return NULL; } arr[ itemsRead++ ] = x; } *N = itemsRead; return arr; } /* * Testing array expansion */ int main( ) { int * arr; int theSum; int i; int N; arr = readInts( &N ); for( i = 0; i < N; i++ ) printf( "Int #%d: %d\n", i, arr[ i ] ); /* Cleanup mess */ free( arr ); return 0; }