# pointer mayhem

Printable View

• 10-18-2007
aliaseer
pointer mayhem
Hi.

I'm trying to understand how pointers work. I have an algorithms implemented in two ways: one using pointers, the other not so much. The thing is, the second implementation works, the first doesn't and I'm not sure why.

Here's my main (IN_FILE, N_DATA and E_GOOD are globally defined variables: name of an input-file, 1000, and the exit code):
Code:

```int main(const int argc, const char *argv){         ifstream fin(IN_FILE);         vector< Supplier >* supps = new vector< Supplier > (N_DATA);                 for(int i = 0; i < N_DATA; ++i)                 fin >> supps->at(i).companyID >> supps->at(i).price;                 printSuppliers( &selectionSort(*supps), 2);         supps = sort(supps);         cout << "<--------------------------->" << endl;         printSuppliers(supps, 2);                 return E_GOOD; }```
Here's the pointer based sorting:
Code:

```vector< Supplier > * sort(vector< Supplier > *v){         int i, j, min;         for (i = 0; i < v->size() - 1; i++){                 min = i;                 for (j = i+1; j < v->size(); j++){                         if (v->at(i).price < v->at(min).price){                                          min = j;                         }                 }                 swap(v->at(i), v->at(min));         }                 return v; }```
and the non-pointer based:
Code:

```vector<Supplier> selectionSort(vector<Supplier> a){         int i, j, min;         for (i = 0; i < a.size() - 1; i++){                 min = i;                 for (j = i+1; j < a.size(); j++){                         if (a[j].price < a[min].price){                                          min = j;                         }                 }                 swap(a[i], a[min]);         }                 return a; }```
here's the printing function:
Code:

```void printSuppliers(const vector< Supplier > *v, int n){         for(int i = 0; i < n; i++){                 cout                         << "Supplier: " << i << endl                         << "\tID: " << v->at(i).companyID << endl                         << "\tPrice: " << v->at(i).price << endl << endl                         ;         } }```
My first output (from the dereferenced sorting) is in correct order, but the second output is unsorted:
Code:

```Supplier: 0         ID: C956         Price: 828 Supplier: 1         ID: C38         Price: 893 <---------------------------> Supplier: 0         ID: C0         Price: 1869 Supplier: 1         ID: C1         Price: 1728```
Anyone care to help me understand why this is happening?
• 10-18-2007
swoopy
Code:

`>                        if (v->at(i).price < v->at(min).price){`
Shouldn't that be j?
Code:

`                        if (v->at(j).price < v->at(min).price){`
• 10-18-2007
aliaseer
Quote:

Originally Posted by swoopy
Code:

`>                        if (v->at(i).price < v->at(min).price){`
Shouldn't that be j?
Code:

`                        if (v->at(j).price < v->at(min).price){`

...yes...yes it should. Thanks for pointing that out, and boy do I feel stupid for missing that. At least I know that my understanding of pointers isn't drastically off.
• 10-19-2007
anon
There is no reason really to allocate the vector itself dynamically (using new), and the usual way to pass it to functions is via references.

For example
Code:

`void selectionSort(vector<Supplier>& a)`
This lets you modify the vector in the function.

I can see that you want selectionSort to return the vector to be used as an argument for another function. It is safe to return a reference to the parameter that you passed in by reference. Thus:
Code:

`vector<Supplier>& selectionSort(vector<Supplier>& a)`
Your non-pointer version is not very efficient: you pass a copy of the vector and you return a copy.