#include <stddef.h>
#include <stdbool.h>
#include <stdio.h>
void call_for_each( void (*fun)( int ), int start, int end )
{
for( int i = start; i < end; i++ )
fun( i );
}
bool contains( int const values[], size_t count, int match )
{
__label__ found;
// note: only supported by gcc, not by g++ or clang
void check( int index ) {
if( values[ index ] == match )
goto found;
}
call_for_each( check, 0, count );
return false;
found:
return true;
}
int main()
{
int const values[] = { 2, 5, 7 };
size_t const count = sizeof(values) / sizeof(int);
for( int i = 0; i < 8; i++ ) {
printf( "contains %d: %s\n", i,
contains( values, count, i ) ? "yes" : "no" );
}
return 0;
}
Comments