I have two arrays of type Region
, both of size 1000, and at every iteration of a loop I want to swap the two of them (by swapping their memory addresses). I was hoping this would work:
Region *swap = (Region*)myRegions;
myRegionsLast = myRegions;
myRegions = (Region[1000])swap;
Line one seems fine. The second and third lines are invalid assignments, as you apparently can't re-assign that type. The third line is also invalid because you can't cast to the type (Region [1000])
. Obviously I don't want to allocate whole new Region[1000] if I can help it. Can someone help me accomplish what I want?
This happens because you cannot assign a whole array at once. You can assign a pointer, however:
Region reg1[1000], reg2[1000];
Region *myRegionsLast = reg1;
Region *myRegions = reg2;
Now your swap routine is going to work without further modifications.
You could also swap arrays one element at a time, but it is going to involve a lot more data copying.
How about creating just a variable of the content of Region type and iterating through it...
for(int i=0; i<1000; i++)
{
tempRegionTypeVariable = myRegionsLast[i];
myRegionsLast[i] = myRegions[i];
myRegions[i] = tempRegionTypeVariable;
}
Hope this helps.. Here "tempRegionTypeVariable" is just a temporary variable or an Object. Not an Array..
A couple other valid answers were posted in the comments:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.