#include 
#include 
#include 

typedef nc_vlen_t row_of_floats;

static const float vlen_0[] = { -999} ;
static const float vlen_1[] = { 10,  11,  12,  13,  14} ;
static const float vlen_2[] = { 20,  21,  22,  23} ;
static const float vlen_3[] = { 30,  31,  32} ;
static const float vlen_4[] = { 40,  41} ;

void
check_err(const int stat, const int line, const char *file) {
    if (stat != NC_NOERR) {
        (void)fprintf(stderr,"line %d of %s: %s\n", line, file, nc_strerror(stat));
        fflush(stderr);
        exit(1);
    }
}

int
main() {/* create vlens.nc */

    int  stat;  /* return status */
    int  ncid;  /* netCDF id */

    /* group ids */
    int root_grp;

    /* type ids */
    int row_of_floats_typ;

    /* dimension ids */
    int m_dim;

    /* dimension lengths */
    size_t m_len = 5;

    /* variable ids */
    int ragged_array_id;

    /* rank (number of dimensions) for each variable */
#   define RANK_ragged_array 1

    /* variable shapes */
    int ragged_array_dims[RANK_ragged_array];

    /* enter define mode */
    stat = nc_create("vlens.nc", NC_CLOBBER|NC_NETCDF4, &ncid);
    check_err(stat,__LINE__,__FILE__);
    root_grp = ncid;

    stat = nc_def_vlen(root_grp, "row_of_floats", NC_FLOAT, &row_of_floats_typ);    check_err(stat,__LINE__,__FILE__);


    /* define dimensions */
    stat = nc_def_dim(root_grp, "m", m_len, &m_dim);
    check_err(stat,__LINE__,__FILE__);

    /* define variables */

    ragged_array_dims[0] = m_dim;
    stat = nc_def_var(root_grp, "ragged_array", row_of_floats_typ, RANK_ragged_array, ragged_array_dims, &ragged_array_id);
    check_err(stat,__LINE__,__FILE__);

    /* assign per-variable attributes */
    { /* _FillValue */
    static const row_of_floats ragged_array_FillValue_att[1] = {{1, (void*)vlen_0}} ;
    stat = nc_put_att(root_grp, ragged_array_id, "_FillValue", row_of_floats_typ, 1, ragged_array_FillValue_att);    check_err(stat,__LINE__,__FILE__);
    }


    /* leave define mode */
    stat = nc_enddef (root_grp);
    check_err(stat,__LINE__,__FILE__);

    /* assign variable data */
    {
    row_of_floats ragged_array_data[5] = {{5, (void*)vlen_1}, {4, (void*)vlen_2}, {3, (void*)vlen_3}, {2, (void*)vlen_4}, {1, (void*)vlen_0}} ;
    size_t ragged_array_startset[1] = {0} ;
    size_t ragged_array_countset[1] = {5} ;
    stat = nc_put_vara(root_grp, ragged_array_id, ragged_array_startset, ragged_array_countset, ragged_array_data);
    stat = nc_put_vara(root_grp, ragged_array_id, ragged_array_startset, ragged_array_countset, ragged_array_data);
    check_err(stat,__LINE__,__FILE__);
    }

    stat = nc_close(root_grp);
    check_err(stat,__LINE__,__FILE__);
    return 0;
}