Lecture 3 - Pointers, Reference and Structured Data
Lecture 3 - Pointers, Reference and Structured Data
Lecture 3
Pointers, References and Structured Data
C.F. Kwong
[email protected]
Department of Electrical and Electronic Engineering
Faculty of Science and Engineering
Room: PMB 325
Topics
Revision of Pointers and References
Structures
Unions
Enumerators
Getting the Address of a Variable
Each variable in program is stored at a unique
address
Use address operator & to get address of a variable:
int num = -99;
cout << # // prints address
// in hexadecimal
Pointer Variables
Pointer variable : Often just called a pointer, it's a
variable that holds an address
Because a pointer variable holds the address of
another piece of data, it "points" to the data
Definition:
int *intptr;
Read as:
“intptr can hold the address of an int”
Spacing in definition does not matter:
int * intptr; // same as above
int* intptr; // same as above
Pointer Variables
Assigning an address to a pointer variable:
int *intptr;
intptr = #
Memory layout:
Something Like Pointers : Arrays
The values parameter, in the showValues
function, points to the numbers array.
4 7 11
starting address of vals: 0x4a00
cout << vals; // displays
// 0x4a00
cout << vals[0]; // displays 4
The Relationship Between Arrays and Pointers
Array name can be used as a pointer constant:
int vals[] = {4, 7, 11};
cout << *vals; // displays 4
Pointer can be used as an array name:
int *valptr = vals;
cout << valptr[1]; // displays 7
Pointers in Expressions
Given:
int vals[]={4,7,11}, *valptr;
valptr = vals;
(Program Continues)
Pointers as Function Parameters in Program 9-11
Pointers to Constants
If we want to store the address of a constant in a
pointer, then we need to store it in a pointer-to-
const.
Example: Suppose we have the following definitions:
Example
struct <structName>
{
type1 field1;
type2 field2;
. . .
};
Example struct Declaration
struct Student Structure tag
{
int studentID;
string name;
Structure members
short yearInSchool;
double gpa;
};
Must have ; after closing }
struct names commonly begin with uppercase
letter
Defining Variables
struct declaration does not allocate memory or
create variables
To define variables, use structure tag as type name:
Student bill;
Assessing Structure Members
Use the dot (.) operator to refer to members of
struct variables:
if (bill.studentID ==
william.studentID) ...
Initialising a Structure
struct variable can be initialized when defined:
Student s = {11465, "Joan", 2, 3.75};
struct Student
{ int studentID;
PersonInfo pData;
short yearInSchool;
double gpa;
};
Members of Nested Structures
Use the dot operator multiple times to refer to fields
of nested structures:
Student s;
s.pData.name = "Joanne";
s.pData.city = "Tulsa";
Structures as Function Arguments
May pass members of struct variables to functions:
computeGPA(stu.gpa);
May pass entire struct variables to functions:
showData(stu);
Can use reference parameter if function needs to
modify contents of structure variable
Excerpts from Program 11-6
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
Revised showItem Function
Returning a Structure from a Function
Function can return a struct:
Student getStudentData(); // prototype
stu1 = getStudentData(); // call
workDay = WEDNESDAY;
workDay = “WEDNESDAY”; ✗
Enumerated Data Types
So, what is an enumerator?
Think of it as an integer named constant
Internally, the compiler assigns integer values to the
enumerators, beginning at 0.
int x;
x = THURSDAY;