zmatt icon

gcc-nested-functions.c

zmatt | PRO | 08/23/18 02:53:09 AM UTC | 0 ⭐ | 537 👁️ | Never ⏰ | []
C |

720 B

|

None

|

0 👍

/

0 👎

#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