Skip to main content
gamedev.net
🔒 Locked

Pointer problem

Started by CProgrammer 4 replies 400+ views
Original Post
CProgrammer
CProgrammer
I have an array: int *array = new int [5]; array[0] = 5; array[1] = 2; array[2] = 8; array[3] = 6; array[4] = 7; Now I assign a pointer as so: int *pointer = &array[2]; Now I sort the array: (std::sort or so): array[0] = 2; array[1] = 5; array[2] = 6; array[3] = 7; array[4] = 8; Now my pointer will point to the value 6 but I want it to continue pointing to the value 8, thus to the value before the sort. PS: Changing the order is not an option. -CProgrammer
miodragsm
miodragsm
Create class for array with new func. & [] operator.Use dinamicly linked list inside the class to store array values like this:

struct SINT
{
int value;
struct SINT * link;
};
class CArray
{
struct SINT * m_pArray;

//sort, [] operator, new func., ...
};

[] operator will return struct SINT * but it will always point to the same value.





[edited by - miodragsm on January 22, 2004 8:33:47 AM]
Master Mofo
Master Mofo
You can''t do that in traditional arrays. You have to always run a search for the value after the sort. Save the data that is pointed to by the pointer, Sort then search for the saved data and make the pointer point to it again. But this is not a practical solution, if you are dealing with large amounts of data and pointers then this will cause some slow down.
Whatever
stubble
stubble
That''s because your pointer is pointing to the array element - not the value contained in the element.

You could have a constant array of your numbers, (A). Then, for sorting, create another array of pointers to int (B) - which point to the ints in your constant array, A. When you sort array B you sort it based on the value that elements *point* to in array A. You can track any element in array B by creating a pointer to a pointer to an int. eg int**

Does that help?
Agony
Agony
Or you could use an array of pointers to ints. It would be really ugly, though but it would work.


int* array[] = new int* [5];
int i;
for (i = 0; i < 5; ++i)
array[i] = new int;

*array[0] = 5;
*array[1] = 2;
*array[2] = 8;
*array[3] = 6;
*array[4] = 7;

int *pointer = array[2];

//Sort


cout <<
*array[0] << " " <<
*array[1] << " " <<
*array[2] << " " <<
*array[3] << " " <<
*array[4] << endl; //"2 5 6 7 8"


cout << *pointer << endl; //"8"


for (i = 0; i < 5; ++i)
delete array[i];

delete [] array;


Just make sure you sort the array based on the values pointed to, not the values of the pointers themselves. Otherwise you''d just get the array sorted by where everything is located in memory.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas nly, and not for things themselves." - John Locke o
CProgrammer
CProgrammer
Agony''s solution was pretty much what I was looking for. Thats fast and handy thanks. I thought of making a copy of the array, sort that, use it then delete it. That way I have the original and the correct pointer but Agony''s solution is much more efficient.
-CProgrammer

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.