Die Struktur definiert einen neuen Datentyp welcher Komponenten unterschiedlichen Typs vereint. Die Typdeklaration
struct <struct_bezeichner>
{
<Datendeklaration>
};
erlaubt die Deklaration von Variablen diesen Typs
<struct_bezeichner> <var_bezeichner>;
Beispiel: Wir deklarieren einen Datentyp zur Speicherung der persönlichen Daten eines Studenten.
// Structure
{
// new structure
struct Student
{
long long int matrikel;
int skz;
char name[30], vorname[20];
};
// Variable of type Student
Student arni,robbi;
// Data input
cout << endl << " Vorname : ";
cin >> arni.vorname;
...
robbi = arni; // complete copy
cout << robbi.vorname << endl;
}
Die Zuweisung robbi = arni; kopiert den kompletten Datensatz von einer Variablen zur anderen. Der Zugriff auf die Komponente vorname der Variablen arni (des Typs Student) erfolgt über
arni.vorname
gesamtes Programm
// Structure
#include <iostream.h>
#include <string.h>
main()
{
// new structure
struct Student
{
long long int matrikel;
int skz;
char name[30], vorname[20];
};
// Variable of type Student
Student arni,robbi;
// Data input
cout << endl << " Vorname : ";
cin >> arni.vorname;
cout << endl << " Familienname : ";
cin >> arni.name;
cout << endl << " Studentenkennzahl : ";
cin >> arni.skz;
cout << endl << " Matrikelnummer : ";
cin >> arni.matrikel;
robbi = arni;
// output
cout << endl << "-----------------------------------" << endl;
cout << robbi.vorname << " " << robbi.name << ", SKZ: ";
cout << robbi.skz << " " << robbi.matrikel << endl << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Abgespeichert werden die Daten in der Form
Abhängig von Compilereinstellungen bzw. -optionen können kleinere ungenutzte Speicherlücken zwischen den Komponenten im Speicher auftreten (Data Alignment für schnelleren Datenzugriff).
Die Struktur Student kann leicht für Studenten, welche mehrere Studienrichtungen belegen, erweitert werden.
{
const int MAX_SKZ=5;
struct Student_Mult
{
long long int matrikel;
int skz[MAX_SKZ];
int nskz; // number of studies
char name[30], vorname[20];
};
// Variable of type Student
Student arni,robbi;
// Data input
cout << endl << " Vorname : ";
cin >> arni.vorname;
...
robbi = arni; // complete copy
cout << robbi.vorname << endl;
}
gesamtes Programm
#include <iostream.h>
#include <string.h>
main()
{
const int MAX_SKZ = 5;
// new structure
struct Student_Mult
{
long long int matrikel;
int skz[MAX_SKZ];
char name[30], vorname[20];
int num_skz;
};
// Variable of type Student
Student_Mult arni,robbi;
int i;
// Data input
cout << endl << " Vorname : ";
cin >> arni.vorname;
cout << endl << " Familienname : ";
cin >> arni.name;
cout << endl << " # skz : ";
cin >> arni.num_skz;
for (i=0; i < arni.num_skz; i++)
{cout << endl << " Studentenkennzahl : ";
cin >> arni.skz[i];
}
cout << endl << " Matrikelnummer : ";
cin >> arni.matrikel;
robbi = arni;
// output
cout << endl << "-----------------------------------" << endl;
cout << robbi.vorname << " " << robbi.name << ", SKZ: ";
for (i=0; i < robbi.num_skz; i++)
{
cout << robbi.skz[i] << " ";
}
cout << robbi.matrikel << endl << endl;
}
system("PAUSE");
return EXIT_SUCCESS;
Die Struktur Student enthält bereits Felder als Komponenten. Andererseits können diese Datentypen wiederum zu Feldern arrangiert werden.
// Array of structures
{
struct Student // new structure
{
...
};
const int N = 20;
int i;
Student gruppe[N]; // Array
// Init
for (i = 0; i < N; i++)
{
cin >> gruppe[i].vorname;
...
}
...
}
gesamtes Programm
// Dynamic array of type student
// data form input-file
#include <iostream.h>
main()
{
struct Student
{
long long int matrikel;
int skz;
char name[30], vorname[20];
};
int i, n;
Student gruppe[4]; //array of Student
cout << endl;
cout << " How many Students : ";
cin >> n; // input n
if ( n <= 4)
{
for (i = 0; i < n; i++)
{
cout << endl << "Student nr. " << i << endl;
cout << "Familenname : ";
cin >> gruppe[i].name;
cout << "Vorname : ";
cin >> (gruppe+i)->vorname;
cout << "Matrikelnummer : ";
cin >> gruppe[i].matrikel;
cout << "SKZ : ";
cin >> gruppe[i].skz;
}
cout << endl;
}
i = 3;
cout << endl;
cout << "Student nr. " << i << " : ";
cout << gruppe[i].vorname << " " << gruppe[i].name << " , ";
cout << gruppe[i].matrikel << " , " << gruppe[i].skz << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
Strukturen können wiederum andere strukturierte Datentypen als Komponenten enthalten.
// Structures in structures
{
struct Point3D // simple structure
{
double x,y,z;
};
struct Line3D // structure uses Point3D
{
Point3D p1,p2;
};
Line3D line; // Declare variable
// Init
cout << "Anfangspkt.: ";
cin >> line.p1.x >> line.p1.y >>line.p1.z;
cout << " Endpkt.: ";
cin >> line.p2.x >> line.p2.y >>line.p2.z;
...
}
gesamtes Programm
// structure in structure
// data form input-file
#include <iostream.h>
#include <math.h>
main()
{
// Define structures
struct Point3D
{
double x,y,z; // Coordinates
};
struct Line3D
{
Point3D p1,p2; // Points
};
// Define variables
Line3D line;
double length;
// Input data
cout << endl;
cout << "Input Line , Point 1 (x,y,z) : ";
cin >> line.p1.x >> line.p1.y >> line.p1.z;
cout << " Point 2 (x,y,z) : ";
cin >> line.p2.x >> line.p2.y >> line.p2.z;
length = sqrt( (line.p2.x-line.p1.x)*(line.p2.x-line.p1.x)
+(line.p2.y-line.p1.y)*(line.p2.y-line.p1.y)
+(line.p2.z-line.p1.z)*(line.p2.z-line.p1.z) );
cout << endl;
cout << " has length " << length << endl;
cout << endl;
}
In obigem Beispiel ist line.p2 eine Variable vom Typ Point3D , auf deren Daten wiederum mittels des . Operators zugegriffen werden kann.