repl.it
@SharkFin/

Pointer Learning

C++11

No description

fork
loading
Files
  • main.cpp
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>

void newline() {
	std::cout << std::endl;
}

template<typename T>
void addOne(T* num) {
	++*num;
}

int main() {
	int arr[8] = {4,3,9,1,5,2,8,3};
	addOne(&arr[0]);

	// C++98 style pointer array loop
  for (int* i=arr;i!=&arr[sizeof(arr)/sizeof(int)];i++) {
		addOne(i);
		std::cout << *i << '-';
	}
	newline();

	// C++11 style pointer array loop
	for (int& i : arr) {
		addOne(&i);
		std::cout << i << '-';
	}
	newline();

	return 1;
}
?