struct structureName
{
// data elements in the structure
};
struct is a keyword
/* A structure representing the parts of a fraction (a rational number) */
struct Fraction
{
int num; // the numerator of the fraction
int denom; // the denominator of the fraction
};
/* A structure representing a record in a student database */
struct Student
{
char fName[20]; // first name
char lName[20]; // last name
int socSecNumber; // social security number
double gpa; // grade point average
};
structureName variableName;Variations on this format include the usual forms for creating arrays and pointers, and the comma-separated list for multiple variables
Fraction f1; // f1 is now a 'Fraction' Fraction fList[10]; // an array of 'Fraction' structures Fraction * fptr; // a pointer to a 'Fraction' structure Student stu1; // a Student structure variable Student mathclass[10]; // an array of 10 Students Student s1, s2, s3; // three Student variables
struct structureName
{
// data elements in the structure
} variable1, variable2, ... , variableN;
struct Fraction
{
int num; // the numerator of the fraction
int denom; // the denominator of the fraction
} f1, fList[10], *fptr; // variable, array, and pointer created
struct // note: no structure NAME given
{
int num;
int denom;
} f1, f2, f3; // three variables representing fractions
struct Date // a structure to represent a date
{
int month;
int day;
int year;
};
struct Employee // a structure to represent an employee of a company
{
char firstName[20];
char lastName[20];
Date hireDate;
Date birthDate;
};
structVariableName.dataVariableName
Fraction f1, f2; f1.num = 4; // set f1's numerator to 4 f1.denom = 5; // set f1's denominator to 5 f2.num = 3; // set f2's numerator to 3 f2.denom = 10; // set f2's denominator to 10 cout << f1.num << '/' << f1.denom; // prints 4/5 cout << f2.num << '/' << f2.denom; // prints 3/10
Student sList[10]; // array of 10 students
// set first student's data: (John Smith, SSN: 123456789, GPA: 3.75)
strcpy(sList[0].fName, "John");
strcpy(sList[0].lName, "Smith");
sList[0].socSecNumber = 123456789;
sList[0].gpa = 3.75;
// assume there's more code here that initializes other students
// This loop prints all 10 students -- their names and their GPA
cout << fixed << setprecision(2);
for (int i = 0; i < 10; i++)
{
cout << sList[i].fName << ' ' << sList[i].lName << ' '
<< sList[i].gpa << '\n';
}
Fraction f1 = {3, 5}; // initializes num = 3, denom = 5
// This would be the same as doing the following:
f1.num = 3;
f1.denom = 5;
Student s1 = {"John", "Smith", 123456789, 3.75};
Student s2 = {"Alice", "Jones", 123123123, 2.66};
Fraction f1; // a fraction structure Fraction *fPtr; // pointer to a fraction fPtr = &f1; // fPtr now points to f1 f1.num = 3; // this is legal, of course fPtr.num = 10; // but how about this? NO! ILLEGAL // cannot put a pointer on the left side // of the dot-operatorRemember that to get to the target of a pointer, we dereference it. The target of fPtr is *fPtr. So how about this?
*fPtr.num = 10; // closer, but still NO (not quite)The problem with this is that the dot-operator has higher precedence, so this would be interpreted as:
*(fPtr.num) = 10; // cannot put a pointer on the left of the dotBut if we use parintheses to force the dereference to happen first, then it works:
(*fPtr).num = 10; // YES!
pointerToStruct -> dataVariableExample:
Fraction * fPtr; // pointer to a fraction // assume this has been pointed at a valid target fPtr->num = 10; // set the fraction's numerator to 10 fPtr->denom = 11; // denominator set to 11 cout << fPtr->num << '/' << fPtr->denom; // prints: 10/11 // Note that fPtr->num is just a shortcut for (*fPtr).num
struct Date // Date is now a type name
{
int month;
int day;
int year;
}; // so that "Date" is the type name
struct Employee
{
char firstName[20];
char lastName[20];
Date hireDate;
Date birthDate;
};
Employee emp; // emp is an employee variable // Set the name to "Alice Jones" strcpy(emp.firstName, "Alice"); strcpy(emp.lastName, "Jones"); // set the hire date to March 14, 2001 emp.hireDate.month = 3; emp.hireDate.day = 14; emp.hireDate.year = 2001; // sets the birth date to Sept 15, 1972 emp.birthDate.month = 9; emp.birthDate.day = 15; emp.birthDate.year = 1972;
Employee emp2 =
{ "John", "Smith", {6, 10, 2003}, {2, 19, 1981} };
// John Smith, whose birthday is Feb 19, 1981, was hired on June 10, 2003