C++: working with pointers

Pointer are a nice way in C++ to run through an array, in this simple example in this article, we create one array with the size of DIM = 10, fill it with random integer values and give it to our helper function to print the content of every field in this array.

Through the pointer arithmetic, we can increment the pointer to the first element of our array, and with the * pointer specifier we get the value. If we would use & instead, the function would print out the address of the value in memory.

// Zeiger.cpp : Definiert den Einstiegspunkt für die Konsolenanwendung.
//

#include "stdafx.h"
#include 
#include 

#define DIM 10

void showArrayContent(int *firstElement, int length) {
	for(int i = 0; i < length; ++i)
		std::cout << *firstElement++ << std::endl;
}
void workWithReference(int *refInt) {
	std::cout << refInt++ << std::endl;
}

int _tmain(int argc, _TCHAR* argv[])
{
	int intArray[DIM];
	for(int i = 0; i < DIM; ++i) {
		intArray[i] = rand();
	}

	showArrayContent(intArray, DIM);

	int a = 10;
	int *iz = &a;
	double b = 12.4;
	double*dz= &b;
	workWithReference(&a);
	std::cout << "a = " << a << " & *iz = " << *iz << std::endl;
	return 0;
}