jamesjuett / lobster

Interactive Program Visualization Tools
8 stars 3 forks source link

Pointer arrows don't update endpoints properly #202

Closed jamesjuett closed 3 years ago

jamesjuett commented 3 years ago

Example:

#include <iostream>
using namespace std;

// This function doesn't work as intended because
// the pass-by-value parameters x and y are copies
// of the original values. So even if x and y are
// swapped, there is no effect on a and b in main.
// YOUR TASK - Fix this by using pass-by-pointer instead.
void swap(int *x, int *y) {
  int temp = *x;
  *x = *y;
  *y = temp;
}

int main() {
  int a = 3;
  int b = 5;
  int *ptr = &b;
  // HINT: You also need to change something about this line
  swap(&a, ptr);

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