Return structure with flexible array member

后端 未结 7 1898
栀梦
栀梦 2021-01-15 03:23

I need to return a structure with a flexible array member from a C function but can\'t figure out why it doesn\'t compile. I know that returning arrays can be achieved by en

7条回答
  •  無奈伤痛
    2021-01-15 04:16

    I use gcc and I think may be this way is ok:

    struct data_array {
        long length;
        double* data;
    };
    struct data_array* test (int length) {
        struct data_array* a =(struct data_array *) malloc (sizeof (data_array));
        a->data = malloc (length*sizeof(double));
        a->length = length;
        return a;
    }
    

提交回复
热议问题