So basically he explained something that actually has happened 2 or 3 times already in problem sets. If I want to switch two variables, usually I need to create a temporary variable to hold a value to make the switch possible.
So to switch a to b, you need to do:
tmp = a
a = b
b = tmp
And in class they used an analogy with 3 cups and need to switch from one to another:
Then seems that you can use XOR (^) to swap 2 variables. You can test it by applying XOR to each bit, bit by bit… you will see that it actually works :D!
Important, also learned that you can represent your computers memory like below. Stack is chunck of memory that functions have access when they are called.
Interesting that environment variables (of main) take a chunk of space, but functions take a different one. When I pass variables to a function, those are a COPY of the original. So what if I want a function to affect the space of the environment variables?
Below is what happens after swaping a and b on function.
This is a very interesting example. Somehow something is wrong.
The interesting thing is that what is stored in any “string” variable, is not the string itself, its the address in memory of that string.
Actually “strings” doesn’t exist. Its just a synonym, for char*. The * is a “pointer”, an “address”. So from now on, instead of calling the type “string”, we will put “char*”