So I've started learning about pointers in C++. On the topic of passing pointers to functions, I've seen two different possible implementations:
1.
#include <stdio.h>
void multiplyBy2(int *num);
int main(int argc, char *argv[]) {
int six = 6;
printf("%d\n", six);
multiplyBy2(&six);
printf("%d\n", six);
}
void multiplyBy2(int *num) {
*num *= 2;
}
2.
#include <stdio.h>
void multiplyBy2(int &num);
int main(int argc, char *argv[]) {
int six = 6;
printf("%d\n", six);
multiplyBy2(six);
printf("%d\n", six);
}
void multiplyBy2(int &num) {
num *= 2;
}
Both of these, when compiled and run, have the exact same output:
6
12
My question is, is there any difference between these to implementations, and is one better than the other?