Passing references to functions to be assigned to pointers in C++, Which way is better? -
i'm wondering 1 better of these 2 implementations of passing addresses pointers. there data exchange in 1st 1 doesn't happen in 2nd one? second 1 more efficient way? more readable? both same? version 1 void ptrfunction(int& arg) { int* ptr = &arg; std::cout<<"the pointer's value:" << *ptr << "\n"; } int main() { int x=5; ptrfunction(x); return 0; } version 2 void callviapointers(int *arg) { int *ptr = *arg; std::cout << "the pointer's value: " << *ptr; } int main() { int x = 100; callviapointers(&x); return 0; } in terms of efficiency, 2 compile down same code behind-the-scenes, references implemented automatically-dereferenced pointers. perspective, 2 solutions equivalent. as whether pass pointer or reference, that's judgment call depends on situation. if you're trying print out pointer integer (as you're doing here), pas...