Trouble Understanding Pointer Assignment

I sent this to my instructor but he hasn't replied back and it's due today so I'd appreciate any input. Unfortunately I haven't even gotten past the pseudo code.

The assignment is: Write a function void sort2(double* p, double* p) that receives 2 pointers and sorts the values to which they point. If you call sort2(&x, &y) then x<=y after the call.

When declaring the original function how come you declare the same pointer twice?

So far I understand that when you call the function you want the memory address of x to be less than or equal to the memory address of y. But why would different memory address be equal?

My pseudo code, which is probably way off, so far is:

a. declare function
b. p points to xaddress
c. if x address is less than or equal to y address than cout x is less than or greater to y
d. else if y address is less than x address than swap:
1. y address = temp
3. temp = x address
4. cout x is less than or greater to y
e. int main
f. call function

I know that displaying the same cout in either situation isn't a good way to test that it isn't working but I'm not sure how else to go about it. Again thanks for any input :)
 When declaring the original function how come you declare the same pointer twice?

I think this was a mistake (or at least they didn't intend it that way).

 So far I understand that when you call the function you want the memory address of x to be less than or equal to the memory address of y. But why would different memory address be equal?

I think you are misunderstanding. They want you to compare the values, not the addresses of the pointers.
That "sort2(double* p, double* p)" should probably read "sort2(double* p, double* q)".

By that example "sort2(&x, &y) then x<=y", you won't sort addresses, but values.
I do not think the function should be:
`void sort(double *p, double *p)`
but rather have on of the parameter's names changed. You should then compare the values of what the pointers point to (not the pointers themselves), and if y>x then swap the values.
As far as I understood the & operator pointed to the address and the * operator pointed to the value. Is that wrong?
operator yes, type qualifier not
If it's double &x then it's an operator? If it's just &x it's a type qualifier? How are the uses of it different?
Here's my code so far, I tried to write it in a way where I could test it to see if it works. It runs, but it doesn't work.

 ``123456789101112131415161718192021222324252627282930`` ``````#include using namespace std; void sort2 (double* p, double* q) { if (q>p) { double temp; *q = temp; *p = *q ; temp = *p; cout << "x was not greater than y, the values have been swapped."; } else { cout << "x is greater than y"; } } int main () { double x =0; double y =0; cout << "Please enter a numbers for x and then a number for y: "; cin >> x; cin >> y; sort2(&x, &y); return 0; }``````
if (q>p) compares the pointers themselves. You want to compare the values they point to, so dereference them:

`if( *q > *p )`

Your swap code isn't right either. The temp should store the value that's being overwritten. You are storing the (uninitialized) temp variable inside your destination object.

Also, take another look at your logic for comparing the two values. Are you thinking of x > y, or y > x?
Last edited on
I think I fixed some of the problems but I'm not sure how to go about storing the value in temp if I don't know what the value is and I'm pointing to the spot where it will go.

 ``123456789101112131415161718192021222324252627282930`` ``````#include using namespace std; void sort2 (double* p, double* q) { if (*q < *p) { double temp = 0; *q = temp; *p = *q ; temp = *p; cout << "x was not greater than y, the values have been swapped."; } else { cout << "x is greater than y"; } } int main () { double x =0; double y =0; cout << "Please enter a numbers for x and then a number for y: "; cin >> x; cin >> y; sort2(&x, &y); return 0; }``````
I suggest you reading this:
http://www.cplusplus.com/doc/tutorial/pointers/
This article quite helped me when I was learning basics of pointers. At the beginning, when you work with pointers, they can be quite complicated and hard to understand, so if you want - and it works for you - you can make few simple programs with only simple purpose "to see what happens". For example, make a program that has pointer to integer, assign some value to it, and then print all of its attributes:
 ``123456`` ``````//necessary code here int* p = 5; cout<<*p<

Then you may try other things, whatever you come up with, and then try to work with 2 pointers, with arrays, mix them together, build functions using pointers... From small things to bigger you will ensure that you understand every concept and you know what you're doing.

Hope it helps :)
I actually have read that tutorial along with my textbook and it's still going over my head :b Thanks for the advice for writing simple programs I think that would help. :)
Topic archived. No new replies allowed.