Chapter 11:
Structured Data
Copyright © 2012 Pearson Education, Inc.
11.2
Combining Data into Structures
Copyright © 2012 Pearson Education, Inc.
Combining Data into Structures
• Structure: C++ construct that allows multiple
related variables to be grouped together
• General Format:
struct <structName>
{
type1 field1;
type2 field2;
. . .
};
Copyright © 2012 Pearson Education, Inc.
Example struct Declaration
struct Student
{
structure tag
int studentID; structure members
string name;
short yearInSchool;
double gpa;
};
Copyright © 2012 Pearson Education, Inc.
struct Declaration Notes
• Must have ; after closing }
• struct names commonly begin with
uppercase letter
• Multiple fields of same type can be in
comma-separated list:
string name,
address;
Copyright © 2012 Pearson Education, Inc.
Defining Variables
• struct declaration does not allocate
memory or create variables
• To define variables, use structure tag as
type name:
bill
Student bill; studentID
name
yearInSchool
**codeblocks gpa
Copyright © 2012 Pearson Education, Inc.
11.3
Accessing Structure Members
Copyright © 2012 Pearson Education, Inc.
Accessing Structure Members
• Use the dot (.) operator to refer to members of
struct variables:
cin >> stu1.studentID;
getline(cin, stu1.name);
stu1.gpa = 3.75;
• Member variables can be used in any manner
appropriate for their data type
11-8
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
Displaying a struct Variable
• To display the contents of a struct
variable, must display each field
separately, using the dot operator:
cout << bill; // won’t work
cout << bill.studentID << endl;
cout << bill.name << endl;
cout << bill.yearInSchool;
cout << " " << bill.gpa;
Copyright © 2012 Pearson Education, Inc.
Comparing struct Variables
• Cannot compare struct variables
directly:
if (bill == william) // won’t work
• Instead, must compare on a field basis:
if (bill.studentID ==
william.studentID) ...
Copyright © 2012 Pearson Education, Inc.
11.4
Initializing a Structure
Copyright © 2012 Pearson Education, Inc.
Initializing a Structure
• struct variable can be initialized when
defined:
Student s = {11465, "Joan", 2, 3.75};
• Can also be initialized member-by-
member after definition:
s.name = "Joan";
s.gpa = 3.75;
Copyright © 2012 Pearson Education, Inc.
More on Initializing a Structure
• May initialize only some members:
Student bill = {14579};
• Cannot skip over members:
Student s = {1234, "John", ,
2.83}; // illegal
• Cannot initialize in the structure
declaration, since this does not allocate
memory
Copyright © 2012 Pearson Education, Inc.
Excerpts From Program 11-3
Copyright © 2012 Pearson Education, Inc.
11.5
Arrays of Structures
Copyright © 2012 Pearson Education, Inc.
Arrays of Structures
• Structures can be defined in arrays
• Can be used in place of parallel arrays
const int NUM_STUDENTS = 20;
Student stuList[NUM_STUDENTS];
• Individual structures accessible using subscript
notation
• Fields within structures accessible using dot
notation:
cout << stuList[5].studentID;
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
11.7
Structures as Function Arguments
Copyright © 2012 Pearson Education, Inc.
Structures as Function
Arguments
• May pass members of struct variables to
functions:
computeGPA(stu.gpa);
• May pass entire struct variables to functions:
showData(stu);
• Write prototypes for the functions
• Can use reference parameter if function needs
to modify contents of structure variable
Copyright © 2012 Pearson Education, Inc.
Excerpts from Program 11-6
Copyright © 2012 Pearson Education, Inc.
Structures as Function
Arguments - Notes
• Using value parameter for structure can
slow down a program, waste space
• Using a reference parameter will speed up
program, but function may change data in
structure
• Using a const reference parameter
allows read-only access to reference
parameter, does not waste space, speed
Copyright © 2012 Pearson Education, Inc.
Revised showItem Function
Copyright © 2012 Pearson Education, Inc.
11.8
Returning a Structure from a
Function
Copyright © 2012 Pearson Education, Inc.
Returning a Structure from a
Function
• Function can return a struct:
Student getStudentData(); // prototype
stu1 = getStudentData(); // call
• Function must define a local structure
– for internal use
– for use with return statement
Copyright © 2012 Pearson Education, Inc.
Returning a Structure from a
Function - Example
Student getStudentData()
{ Student tempStu;
cin >> tempStu.studentID;
getline(cin, tempStu.name);
getline(cin, tempStu.address);
getline(cin, tempStu.city);
cin >> tempStu.yearInSchool;
cin >> tempStu.gpa;
return tempStu;
}
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
Copyright © 2012 Pearson Education, Inc.
11.9
Pointers to Structures
Copyright © 2012 Pearson Education, Inc.
Pointers to Structures
• A structure variable has an address
• Pointers to structures are variables that
can hold the address of a structure:
Student *stuPtr;
• Can use & operator to assign address:
stuPtr = & stu1;
• Structure pointer can be a function
parameter
11-35
Copyright © 2012 Pearson Education, Inc.
Accessing Structure Members
via Pointer Variables
• Must use () to dereference pointer
variable, not field within structure:
cout << (*stuPtr).studentID;
• Can use structure pointer operator to
eliminate () and use clearer notation:
cout << stuPtr->studentID;
11-36
Copyright © 2012 Pearson Education, Inc.
From Program 11-8
Copyright © 2012 Pearson Education, Inc.