When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
45 messages · Page 1 of 1 (latest)
When your question is answered use !solved to mark the question as resolved.
Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.
Bello please help I dont understand pass by pointer,how did they swap or pass the memory addresses without using and percent "&"?
I can only notice the function call so that's progress i think.
!sc
They're hard to read and prevent copying and pasting.
@ruby plover
Oh ohk
@ruby plover
Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.
@worthy acorn ohkay here it is
!f
#include <iostream>
using namespace std;
Void swap(int* x, int* y) {
Int* temp = x;
x = y;
y = temp;
}
Int main() {
int x = 5;
int y = 10;
cout << x << endl cout << y
<< endl
swap(x, y)
cout
<< x << endl cout << y << endl
}
return 0;
Uh
Well, Int and Void aren't going to be understood by the compiler
Similarly you are missing semicolons in main which is why the formatting looks all weird here
and you have a return outside of a function
!format
```cpp
#include <iostream>
using namespace std;
void swap(int* x, int* y) {
int* temp = x;
x = y;
y = temp;
}
int main() {
int x = 5;
int y = 10;
swap(x, y);
cout << x << endl;
cout << y << endl;
return 0;
}
Looks almost perfect
Oh ohkay thanks (hope you not insinuating another re-edit)
@ruby plover Has your question been resolved? If so, run !solved :)
No lol
in the standard library is a function called std::swap because of your use of using namespace std; its just swap, same as your written function, so actually std::swap is called since it has a version taking the parameters you are giving to it
So its not just a name its a command?
Like does it know that its really swapping?
@worthy acorn you there?
Yes it does
Sorry for late response
What issue are you currently running into?
Hello please help I dont understand pass by pointer,how did they swap or pass the memory addresses without using and percent "&"?
I can only notice the function call so that's progress i think.
@worthy acorn this one
Sorry for being late again im here now I switched on my notifications
@ruby plover pointers are just the address of data in memory, use &variable to get the address of a variable and *ptr to get or set memory at an address
Yes but they swapped them here if you compile and run
You get
x=5
y=10
x=10
y=5
Also I know a bit about pointers just that I dont understand how they swapped them here
Well in the code above the swap isn’t actually right
Oh I see whats the matter??
Try running it and see what you get
To be honest I saw it on YouTube and the person got this@worthy acorn
Try running the code yourself, play around with it 😉
Whats the goal here?
Coz you said this the swap is not right
The goal is to help you see why it’s wrong and better understand the code and reason through it @ruby plover
Oh ohk my bad
Playing around with things is an essential part of of building intuition and understanding