I had such code to copy three 2D arraya around by using pointers like below, but it seems all three pointers are pointing to the same memory address. How can I get it correctly?

float **U_past, **U_future, **V_past, **V_future, **U_pp, **V_pp;
In construct :
U_past = new float*[a];   //a=100
U_future = new float*[a];
U_pp = new float*[a];


for(int i=0; i<nsigma; i++) {
U_past = new float[e];     //e=400
U_future = new float[e];
U_pp = new float[e];
}

and in main fuction I want to do

U_pp = U_past;                //save U_past data in U_pp
U_past = U_future;           //save U_future data in U_past
U_future = new_data;       //get new data for U_future

in destruct

delete [] U_past;
delete [] U_future;
delete [] V_pp;

The expect data would be like

first aound : U_pp[0][0]=0.1, U_past[0][0] =0.2, U_future[0][0]=0.3;
second round : U_pp[0][0]=0.2, U_past [0][0]=0.3, U_future[0][0]=1.4;
thrid : U_pp[0][0]=0.3, U_past [0][0]=1.4, U_future[0][0]=2.3;

How should I make it? Thanks.

Recommended Answers

All 3 Replies

and in main fuction I want to do

U_pp = U_past; //save U_past data in U_pp
U_past = U_future; //save U_future data in U_past
U_future = new_data; //get new data for U_future

Arrays cannot be copied or assigned: you must copy each array element explicitly (or use an equivalent such as memcpy).

I tried this two methods, they both worked. But at the same time, when I referral the data in other class, the data wouldn't be accessed. DO you why?

But at the same time, when I referral the data in other class, the data wouldn't be accessed. DO you why?

I don't know what you are trying to say here; could you please clarify your question (preferably including relevant code snippets within code tags

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.