You can have only one flexible member of the array in the structure, and it should always be the last member of the structure. In other words, in this case, you made a mistake before you call malloc , to such an extent that there really is no way to correctly call malloc for this structure.
To do what you think is needed (arrays of the same number of data and label members), you can consider something like:
struct my_pair { double data; int label; }; typedef struct { size_t N; struct my_pair data_label[]; };
Note that this is somewhat different: instead of a double array followed by an int s array, it gives an array of one double , followed by one int , then the next double , then int , etc. Whether it will be close enough to the same or not will depend on how you use the data (for example, to move to an external function that expects a continuous array, you will probably have to do something different).
Jerry Coffin
source share