E
Zeiger sind vor allem für dynamische Speicherallokation (new/delete) wichtig:
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
using namespace std;
int main()
{
unsigned n; // Anzahl Werte
double s; // Summe
int * pa; // Zeiger auf dynam. Array
cout << "Wie viele Zufallswerte? ";
cin >> n; // Erst zur Laufzeit ermittelt !!!
pa = new int[n]; // dynamisches Array zur Laufzeit anfordern !!!
srand( unsigned( time(0) ) );
for (int i=0; i<n; i++)
{
pa[i] = rand() % 99 + 1 ; // Zufallszahlen 1 ... 99 generieren
//cout << pa[i] << ' ';
}
s = 0;
for( unsigned i=0; i<n; ++i )
s += static_cast<double>( pa[i] );
cout << endl << "Mittelwert: " << s / n << endl; // Erwartung: 50.0
delete[] pa; // Speicher für dynamisches Array freigeben.
pa = NULL; // Zur Sicherheit gelöschten Zeiger auf NULL setzen.
getch();
}
#include <iostream>
#include <conio.h>
using namespace std;
void textcolor(int color)
{
SetConsoleTextAttribute( ::GetStdHandle(STD_OUTPUT_HANDLE), color );
}
class X
{
public:
X() // Konstruktor
{
textcolor(LIGHTGREEN);
cout << "ctor von X\n";
}
~X() // Destruktor
{
textcolor(LIGHTRED);
cout << "dtor von X\n";
}
};
int main()
{
{
int n;
cout << "Anzahl Objekte?\n";
cin >> n;
X * pX = new X[n];
textcolor(WHITE);
cout << "Speicheradressen (Heap):" << endl;
for( int i=0; i<n; ++i )
cout << pX+i << endl;
delete[] pX; // Fehler ist einfaches delete anstelle delete[]
pX = NULL; // Null-Zeiger schafft zusätzliche Sicherheit!
}
getch();
}
Ansonsten hier Pointer, Referenzen bei Funktionen im Vergleich:
#include <iostream>
#include <conio.h>
using std::cout;
using std::endl;
//globale Variablen
int a = 1;
int b = 2;
void ausgabe()
{
cout << &a << ": " << a << "\t " << &b << ": " << b << endl << endl;
}
void erfolgloser_swap( int x, int y )
{
cout << "Kopien auf dem Stack: " << endl;
cout << &x << ": " << x << "\t " << &y << ": " << y << endl;
int temp = x;
x = y;
y = temp;
cout << &x << ": " << x << "\t " << &y << ": " << y << endl;
cout << "swap nur lokal erfolgreich." << endl;
}
void zeiger_swap( int * x, int * y ) // C-Lösung
{
int temp = *x; // Inhalts-Operator // Dereferenzierungs-Operator
*x = *y;
*y = temp;
}
void referenzen_swap( int & x, int & y ) // C++-Lösung
{
int temp = x;
x = y;
y = temp;
}
int main()
{
cout << "Ausgangssituation: " << endl;
ausgabe();
cout << "Tausch mittels std::swap(x,y): " << endl;
std::swap(a,b); // Methode der Wahl aus der STL
ausgabe();
cout << "Tausch mittels Zeiger: " << endl;
zeiger_swap(&a, &b);
ausgabe();
cout << "Tausch mittels Referenzen: " << endl;
referenzen_swap(a,b);
ausgabe();
cout << "Erfolgloser Tausch, da nur lokale Kopien getauscht werden." << endl;
erfolgloser_swap(a,b);
ausgabe();
/*
cout << "Tausch mittels Register eax und ebx: " << endl;
//swap (a,b)
__asm("mov _b, %eax"); //AT&T Syntax bei Dev-C++
__asm("mov _a, %ebx");
__asm("mov %eax, _a");
__asm("mov %ebx, _b");
ausgabe();
*/
getch();
}
// Beispiel: Fernbedienung
#include <iostream>
#include <iomanip> // setw(...)
#include <string>
using namespace std;
void kanal_anzeige( string * kanal, char eingabe );
int main()
{
string kanal[] = {"ARD","ZDF","RTL","HR3","SWF3","BR3","MDR","CNN","3SAT"};
const int N = sizeof( kanal ) / sizeof( kanal[0] ); // Anzahl = Größe Array / Größe Element
for( int i=0; i<N; ++i )
cout << "Kanal " << setw(2) << i+1 << " :" << kanal[i] << endl;
cout << endl;
while(true)
{
char eingabe, letzte_eingabe;
letzte_eingabe = eingabe;
cout << "Eingabe: ";
cin >> eingabe;
if( ( eingabe>'0' ) && ( eingabe <= static_cast<char>(N+48) ) ) // ASCII 48 ist '0'
{
kanal_anzeige( kanal, eingabe );
}
else if (eingabe == '+')
{
eingabe = letzte_eingabe+1;
kanal_anzeige( kanal, eingabe );
}
else if (eingabe == '-')
{
eingabe = letzte_eingabe-1;
kanal_anzeige( kanal, eingabe );
}
else if (eingabe == '0')
break;
else
continue;
}
}
void kanal_anzeige( string * kanal, char eingabe )
{
cout << "Gewaehlter Kanal: " << eingabe << " - "
<< *(kanal + static_cast<int>(eingabe-1-48)) << endl;
}