Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Linking two arrays??

Status
Not open for further replies.

Agent89

Technical User
Aug 5, 2003
2
US
is there a way to link two different arrays? For example, if I change the order of one the arrays, the other changes accordingly?
 
In C/C++ an array is just contiguous memory so if you need more than that you have to write the code yourself.

-pete
 
.. so obvious approach is to reserve a lump of memory as big as the two arrays together, and copy each of the arrays into it by whatever means you like. Then free the memory of the old two arrays if desired.
 
I think Agent89 doesn't mean combining the two arrays together. If you want to change both arrays accordingly, then whatever you do to the one array you must do to the other, for example:
Code:
 #define MAX 10
 int a[MAX], b[MAX];
  
 a[1] = b[1] = 2;
Alternatively, you could just update array b when array a is changed, for example:
Code:
 for (count = 0; count < MAX; count++)
   b[count] = a[count];
[code]
I don't know whether that is what you meant.

    [b]Adonai[/b]
 
I don't know if this is a solution to the particular problem you have, but you can have an array of std::pair<Type1,Type2> (or any other &quot;tuple-ish&quot; type) and manipulate that array. Then, you have to move each pair together, so the order of your &quot;two&quot; arrays stay consistent.
 
Thank you for your input, I think I might have figured it out
 
You can also have an array of pointers to a structure of your choice, containing as many data elements as you want. If you sort the array, you change all the pointers, and all the data are maintained together.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top