#include <iostream>
void swap (int *a, int *b) {
int *k = a;
a = b;
b = k;
}
int main() {
int alpha = 5;
int beta = 34;
swap (alpha,beta);
std::cout << alpha << std::endl;
std::cout << beta << std::endl;
int *ab = new int();
*ab = 34;
int *cd = new int();
*cd =64;
swap(ab,cd);
std::cout << *ab << std::endl;
std::cout << *cd << std::endl;
}
First Question: How am I able to pass values to a function which has pointers as passing parameters.
Second Question: How is it able to swap. It swaps the pointers. But when it comes out shouldn't the values of alpha and beta still remain same.
Third Question: Why doesn't the function work when I pass pointers but works when I pass regular variables.
Fourth Question: In the function void swap(int *a, int* b)
are int *a
and int *b
references?