Structs Structs combine multiple variables into one Structs are user-defined datatypes defining the data type: struct person { string firstname; string lastname; char middleinitial; long int ssn; }; generic: struct typename { variabledefinitions }; using the datatype person max; // just like any other datatype max.firstname = "Max"; // access members with a dot (.) max.middleinitial = '-'; // initialization: has to be in the same order! person john = {"John","Lastname",'X',123456}; Partial initialization of structs is possible! person johnp = {"John","Lastname",'X'}; (Works the same as in arrays, everything not initialized gets set to 0) Assignment of structs is possible: max = john; // overwrites every member of max with the information from john Pointers to structs person* pointermax; // declare a pointer to a struct pointermax = new person; // allocate space // normally do either: (*pointermax).firstname = "Max"; // assign one element pointermax->lastname = "Berger"; // special notation for pointers to structs // or: (*pointermax) = max; // assign the whole thing // end either / or delete pointermax; // free memory Complete example: #include #include using namespace std; struct person { string lastname; string firstname; long ssn; }; person readInAPerson() { person p; cout << "Enter the first name: "; cin >> p.firstname; cout << "Enter the last name: "; cin >> p.lastname; cout << "Enter the SSN: "; cin >> p.ssn; return p; } void printAPerson(person toPrint) { cout << toPrint.firstname << " " << toPrint.lastname << ": " << toPrint.ssn; cout << endl; } int main() { cout << "Enter person 1" << endl; person p1; p1 = readInAPerson(); cout << "Enter another person:" << endl; person p2; p2 = readInAPerson(); printAPerson(p1); printAPerson(p2); return 0; }