I want to pass an array to a constructor and use its elements to fill in a dynamic array. However, I cannot understand how to go about using this array in the constructor.
I have made a function that returns a pointer to this array but I cannot use it inside the constructor for the object I am trying to create.
struct PCB
{
private:
int * ptr;
public:
PCB(int * array)
{
ptr=new int[3];
for(int i=0;i<3;i++)
{
*(ptr+i)=*(array+i);
}
}
};
int * returnPtr()
{
int blockArr[]={21,2,3};
return blockArr;
}
int main()
{
PCB * pcb=new PCB(returnPtr());
}
This code gives me a "segmentation fault" error using visual studio code. I simply want to be able to copy the elements of the array into the dynamic array. Where have I messed up?
std::vectorandstd::array?