Rohit Banda Rohit Banda - 9 days ago 6
C++ Question

Changing the size of an array inside of a struct C++

I have a structure that is shown below.

struct thread_data{
int staringPoint;
int endingPoint;
double query[];
};


The user enters a number and I need that entered in number to be the size of the array. Is there any way to either allocate memory for the array or set the size of that array after the user enters the number?

I tried to do it like this:

int userNumber = 10;
struct thread_data newThreads[5];


for(int i=0; i < 5;i++){
newThreads[i].query = new double[userNumber];
}


but I get this error message:

array type 'double []' is not assignable

Answer

You allocate it right but change double query[]; to double *query;