c-style icon indicating copy to clipboard operation
c-style copied to clipboard

about using structs to pass arguments

Open izabera opened this issue 9 years ago • 0 comments

your example with run_server( "3490" ); doesn't work if the struct has default arguments

#include <stdio.h>
struct run_server_options {
    char * port;
    int backlog;
};

#define run_server( ... ) \
    run_server_( ( struct run_server_options ){ \
        /* default values */ \
        .port = "45680", \
        .backlog = 5, \
        __VA_ARGS__ \
    } )

int run_server_( struct run_server_options opts )
{
    printf("port: %s backlog: %d\n", opts.port, opts.backlog);
}

int main( void )
{
    return run_server( "3490" );
}

this code adds too many elements to the struct "3490" is totally ignored and the output is port: 45680 backlog: 5

izabera avatar Mar 23 '16 10:03 izabera