typedef struct { int N, M; char *posns. } POSNS; POSNS *initGenerator( int N, int M ) { POSNS *p = malloc( sizeof( POSNS ) ); p->N = N; p->M = M; p->posns = malloc( M ); for( i = 0; i < M; ++i ) p->posns[ i ] = N - M + i; return posns; } char *iterGenerator( POSNS *p ) { if( done) { free( p->posns ); free( p ); return NULL; } // modify p->posns; ... return p->posns; } POSNS p = initGenerator( n, m ); while( c = iterGenerator( p ) ) { // use c[]. }