Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 10 additions & 13 deletions src/pointers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,24 @@

using namespace std;

void myswap(int * ptr1, int* ptr2)
void myswap(int** ptr1, int** ptr2)//pointers to pointers must be passed to the function
{
auto temp = ptr1;
ptr1 = ptr2;
ptr2 = temp;
int *temp = *ptr1;
*ptr1 = *ptr2;
*ptr2 = temp;
}


int main()
{
int a = 25, b = 11;
int a = 25, b = 11;
int* pa = &a, * pb = &b;//initialize the pointers with which we will work

cout<<"What am I doing wrong ☹ \n\n";
cout << "Now i'm doing everything right :) \n\n";

cout<<"a = "<<a<<", b = "<<b<<endl;

//swap(a,b); //why does theirs work????

myswap(a, b); // but mine doesn't ?!?!?!?!!!???

cout<<"a = "<<a<<", b = "<<b<<endl;
cout << "a = " << *pa << ", b = " << *pb << endl;

myswap(&pa, &pb); // now we pass the addresses of the pointers with which we work to the function

cout << "a = " << *pa << ", b = " << *pb << endl;
}