int main()
{
int first = 1;
int second = 2;
int third = 3;
cout << &first << endl; //address same
cout << first << endl; //a
cout << second << endl; //b
cout << third << endl; //c
pointer(&first, &second, &third); //change a b c to b c a (a=b b=c c=a)
cout << &first << endl; //address same
cout << first << endl; //b
cout << second << endl; //c
cout << third << endl; //a
It doesnt seem to work like I want it to work
//change a b c to b c a (a=b b=c c=a)
You are being passed addresses of variables whose values you want to swap, correct? You are losing those addresses by overwriting them with the addresses of a, b, and c in your function. I don't think that's what you were meaning to do.
#include <iostream>
void pointer(int * a_ptr, int * b_ptr, int * c_ptr)
{
// make a copy of the value that a_ptr points to
int temp = *a_ptr;
// assign the value that b_ptr points to to the location
// that a_ptr points to
*a_ptr = *b_ptr;
// assign the value that c_ptr points to to the location
// that b_ptr points to
*b_ptr = *c_ptr;
// finally, asign the value stored in temp to the
// location pointed to by c_ptr
*c_ptr = temp;
}
int main()
{
int first = 1;
int second = 2;
int third = 3;
std::cout << &first << std::endl; //address same
std::cout << first << std::endl; //a
std::cout << second << std::endl; //b
std::cout << third << std::endl; //c
pointer(&first, &second, &third); //change a b c to b c a (a=b b=c c=a)
std::cout << &first << std::endl; //address same
std::cout << first << std::endl; //b
std::cout << second << std::endl; //c
std::cout << third << std::endl;
return 0;
}