Monday, March 21, 2016

Structure containing zero length member

It is called unwarranted chumminess with the C implementation
This shows how to use the structure containing zero element

struct test
{
    int len ;
    char str[1] ;
} ;

struct test* fun(char *name)
{
    struct test *ptr = malloc(sizeof(struct test)) ;
    if (ptr)
    {
        ptr->len = strlen(name) ;
        ptr->str = malloc(ptr->len+1) ;
        strcpy(ptr->str, name) ;
    }
    return ptr ;
}

No comments:

Post a Comment