Sign in to follow this  

Pointer To Arrays are not keep value

This topic is 4750 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

My problem for this program I am currently working on is I am trying to Sort an array of structures by the alphabetical order of the string. I have it to the point that I pass in a pointer to the first array create a temp array sort the temp array and try to copy the address of the temp array back to the first array. But it seems when I exit the function and print it out it is as if nothing is changed. Sorry I am not 100% sure how to post code on this forum so forgive me if the formatting is incorrect. If I tried to print out the Array sort it would not be sorted for some reason. I print out the address each time to make sure that it is equal correctly just incase i made an mistake it matches up perfectly till it exits the function. I dont want to post my full source code because it is pretty large. What I am providing is enough source code that you can get an idea of what is going on.
struct Employee // How the struct look like
{
public:
	string Name;

}; 

 sortEm(&Test[0]); //call to the function
 for(int x = 0;x<10;x++) // just printing out the address the address of Test 5 
                         // And Test 4 are completely different i dont konw why
 {


		 cout << "Test Five: "<< &Test[x] << endl;
 }

void sortEm(Employee*  Sort)
{


	 Employee* Temp ,*Temp2;
	 Employee *Sorting[10];
	 cout << "SORT FIRST: "<< Sort << endl << endl;
	 Temp2 = Sort;
	 for(int x = 0;x<10;x++)
	 {

		 Sorting[x] = Temp2;
		 		 cout << "SORT SECOND: "<< Sorting[x] << endl;
		 cout << "TEMP SECOND: "<< Temp2 << endl;
		 Temp2 = ++Temp2;

	 }

	for(int a = 1;a<10;a++)
	for(int b=10-1;b>=a; b--)
	{
		if(Sorting[b-1]->Name > Sorting[b]->Name)
		{
			Temp = Sorting[b-1];
			Sorting[b-1] = Sorting[b];
			Sorting[b] = Temp;
		}
   }
	 for(int x = 0;x<10;x++)
	 {

		 Temp2 = Sorting[x];
		 cout << "SORT Third: "<< Sorting[x] << endl;
		 cout << "TEMP Third: "<< Temp2 << endl;
		 Temp2 = ++Temp2;

	 }
	cout << endl << endl;
    cout << Sort << endl;
	cout << Sorting[0] << endl << endl << endl;
	Temp2 = Sort;
	 for(int x = 0;x<10;x++)
	 {

		 Temp2 = Sorting[x];
		 cout << "SORT Four: "<< Sorting[x] << endl;
		 cout << "TEMP Four: "<< Temp2 << endl;
		 Temp2 = ++Temp2;

	 }
}

Share this post


Link to post
Share on other sites
Just out of interest what's wrong with this:



class Employee
{
std::string name_;
public:
friend bool operator<(const Employee& in1, const Employee& in2);
}

bool operator<(const Employee& in1, const Employee& in2)
{
return (in1.name_ < in2.name_);
}


//Later on:
std::vector<Employee> myEmployees;
// Fill myEmployees
std::sort(myEmployees.begin(), myEmployees.end());
// Note: need to #include <algorithm> to use std::sort



If you specifically want to make your own code work then ignore me, but if you're just looking for a way to do a sort then this should work.

Jim.

PS : To add source code in a neat little box use : [ source ] and [ /source ] without the spaces.

Share this post


Link to post
Share on other sites
Two solutions, using a functor

#include <string>
#include <algorithm>
#include <functional>

struct Employee
{
std::string Name;
};

struct CompareEmployeeName
: public std::binary_function<Employee, Employee, bool>
{
inline bool operator()(const Employee& lhs, cost Employee& rhs) const
{
return lhs.Name < rhs.Name;
}
};

int main()
{
Employee Test[50];

// Insert code to populate the array

std::sort(&Test[0], &Test[50], CompareEmployeeName());
};




Or using a function (less efficient)

#include <string>
#include <algorithm>

struct Employee
{
std::string Name;
};

bool CompareEmployeeName((const Employee& lhs, cost Employee& rhs)
{
return lhs.Name < rhs.Name;
}

int main()
{
Employee Test[50];

// Insert code to populate the array

std::sort(&Test[0], &Test[50], CompareEmployeeName);
};


Share this post


Link to post
Share on other sites
Mistakes in your code:

  • Temp2 = ++Temp2; ++ already modifies the variable

  • Employee *Sorting[10]; why is it an array of pointers, it doesn't match Sort, Temp nor Temp2

  • Temp2 = Sorting[x]; you are modifying the pointer, not what it points to - the original array is not modified. And if it were modified, considering that you don't have a copy of the data (only pointers to it), thea copy-over will not be done properly (once you've written over a memory location, the original will be gone, even if you need to copy it later to it's correct position)

Share this post


Link to post
Share on other sites
Quote:
Original post by JimPrice
Fruny : any reason you'd use a functor as opposed to a straight operator< overload? Is it just efficency gains?

Jim.


What does it mean for an Employee to be less than another? You only have one operator< to overload (for that class), and it doesn't make it apparent whether you are sorting by name (first? last?), employee ID, salary, karma, date of birth...

For a structure which may be sorted in many different way, overloading operator< is a bad idea. Having an explicit function (or functor, for efficiency, since operator() will be inlined, while passing the address of the function to sort will call through the pointer) makes your code more readable and maintainable by better demonstrating the programmer's intent.

Share this post


Link to post
Share on other sites

This topic is 4750 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this