#pass by pointer

45 messages · Page 1 of 1 (latest)

strong tangleBOT
#

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.

ruby plover
#

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.

worthy acorn
#

!sc

strong tangleBOT
# worthy acorn !sc
Zelis
Please Do Not Send Screenshots!

They're hard to read and prevent copying and pasting.

worthy acorn
#

@ruby plover

ruby plover
strong tangleBOT
#

@ruby plover

Please Do Not Delete Posts!

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.

ruby plover
#

@worthy acorn ohkay here it is

worthy acorn
#

!f

strong tangleBOT
#
#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;
Oppenheimer
worthy acorn
#

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

ruby plover
#

!format

strong tangleBOT
#
```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;
}
Oppenheimer
worthy acorn
#

Looks almost perfect

ruby plover
strong tangleBOT
#

@ruby plover Has your question been resolved? If so, run !solved :)

craggy basalt
ruby plover
#

Like does it know that its really swapping?

ruby plover
#

@worthy acorn you there?

worthy acorn
#

Yep

#

Does your code compile?

ruby plover
ruby plover
worthy acorn
#

What issue are you currently running into?

ruby plover
ruby plover
#

Sorry for being late again im here now I switched on my notifications

worthy acorn
#

@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

ruby plover
ruby plover
worthy acorn
#

Well in the code above the swap isn’t actually right

ruby plover
#

Oh I see whats the matter??

worthy acorn
#

Try running it and see what you get

ruby plover
worthy acorn
#

Try running the code yourself, play around with it 😉

ruby plover
ruby plover
worthy acorn
#

The goal is to help you see why it’s wrong and better understand the code and reason through it @ruby plover

worthy acorn
#

Playing around with things is an essential part of of building intuition and understanding