Hello @s1d_3,
I’m quite busy with univ, but, if you’re using C++ to accomplish your task, and if you’d like to chat about it, maybe you can e-mail me at olivbruno8 at gmail dot com and I can reply whenever I have time, even if it’s just to discuss small examples.
What I meant is that you can use “pass by reference” and “pass by pointer” to swap two values for example.
They both do the same thing, but, they use different notations:
#include <iostream>
using namespace std;
//pass by reference
void swap(int & x, int & y)
{
int tmp;
tmp = y;
y=x;
x=tmp;
}
//pass by pointer
void swap2(int *x, int *y)
{
int tmp;
tmp = *y;
*y = *x;
*x = tmp;
}
int main()
{
int x = 4;
int y = 1;
int z = 7;
int t = 2;
swap(x,y);
swap2(&z,&t);
cout << x << " " << y << endl;
cout << z << " " << t << endl;
return 0;
}
On the swap procedure, you pass as parameters to it the references of the values which are stored at the locations pointed by x and y, which means that the pointers are “transparent” to the function and you don’t need to use the * symbol.
When the references get swapped, the identifiers remain the same, x identifies x and y identifies y, but the values are swapped, as intended.
On the swap2 procedure however, we pass as parameters to it, the actual memory addresses where the values are located, and we swap the memory addresses where they are located, so the values also change.
Since values in memory are referenced by their memory locations, the 2 above procedures are the same,as swapping references to values or swapping memory locations is basically the same thing (I hope I’m saying right things here…).
The only significant difference is the syntax used, as imo, the 1st one is more clear if you are aware of the power and meaning of the & symbol.
Best,
Bruno