Chapter - 7
Chapter - 7
Structures in C++
Outline
• Intro
What is structure and why?
• Declaration of a structure
• Declaration and initialization of structure members
• Accessing structure Members
• Struct Operations
• Arrays of structures
• Structures as Function Arguments
• Pointer to structure
• Linked list
Introduction to structure
• What is structure and why?
• Structure in C++ programming language is a user defined data
type that groups logically related information of different data
types (possibly of same type) into a single type.
• It is heterogeneous in that it can be composed of data of
different types.
– In contrast, array is homogeneous; it contains only data of
the same type.
• Structure is a user-defined data type which allows us to
combine data of different types together.
• Suppose you are required to write a program to store Student
information, which will have Student’s
– name, age, department, address, father's name etc,
– which included string values, integer values etc (different
types), can you use array? Yes but it will be a daunting task
and inefficient code.
• Alternative solution: use structure.
Introduction…
• Syntax:
struct [structureName(tag)]
{
type memberName-1;
type memberName-2;
….
type memberName-n
};
Declaration of a structure…
Structure members
Structure variables…next
slides
Declaration…
• The term Student is a structure type name;
– It defines a new data type that’s a data structure of the declared form.
Structure variable,
An object birth of type
Date declared locally
Output:
My birth date is 12/28/86
Example:
Output:
The net pay for employee 6782 is $361.66
Structures as Function Arguments…
• An alternative to the pass-by-value function call in the above
program , in which the called function receives a copy of a
structure, is a pass by reference that passes a reference to a
structure.
• Doing so permits the called function to access and alter values
directly in the calling function’s structure variable.
• For example, referring to the previous program , the prototype
of calcNet() can be modified as the following:
Example(next slides)
Pointer to structure…Example1
Pointer to structure…Example2
Accessing structure members using pointers
• Structure members can be referenced using a pointer in two
ways:
Syntax:
1. (*pointer).memberName //dot operator
2. pointer-> memberName //arrow operator
• Either expression can be used to locate the member. For example,
the following expressions are equivalent:
(*pt).idNum can be replaced by pt->idNum
(*pt).payRate can be replaced by pt->payRate
Which one to use?
• we enclose *pointer in brackets when using (*pointer).member
since the dot (.) operator has higher precedence than the
dereference (*) operator. So, it obvious that it is easier to make
mistakes .Hence the arrow operator is preferable.
Linked List
• A linked list is a linear dynamic data structure to store data items.
• Recall that arrays is also a linear data structure that store data items in
contiguous locations.
• Unlike arrays, linked list does not necessarily store data items in contiguous
memory locations.
• A linked list is composed of elements known as "Nodes" that are divided into
two parts.
• The first component is the part where data is stored, and the second is a part
where we store the pointer to the next node.
• This type of structure is known as a "singly linked list.“
• There are different types of linked list such as singly linked list, doubly linked
list, but, in this session, only singly linked list will be introduced.
Linked List
• The following diagram shows a logical structure of a singly linked list.
head tail
100 NULL
• As shown above, the first node of the linked list is called “head” while the last node
is called “tail”.
• The last node of the linked list will have its next pointer as null since it will not have
any memory address pointed to.
• The numbers below the nodes(100, 200, …) represent the memory address of the
respective nodes. This is just for an example; memory addresses are unsigned
hexadecimal numbers( integers).
• Notice that each preceding node contains memory address of the immediate next
node so that data elements in the linked list do not need to be stored in contiguous
locations.
• head pointer is mandatory but tail pointer is optional for singly lined list.
Linked list vs Array
• Advantages of Linked List:
• Better use of Memory
– From a memory allocation point of view, linked lists are more efficient
than arrays.
– Unlike arrays, the size for a linked list is not pre-defined, allowing the
linked list to increase or decrease in size as the program runs
(dynamically).
• Fast Insertion/Deletion Time
– inserting a new node to the beginning or end of a linked list takes
constant time (O(1)).
• Disadvantages of Linked List
• Slower Search Time
– Linked list have slower search times than arrays as random access is not
allowed. (Linked list is a sequential access data structure whereas array is
a random access data structure).
Linked list VS Array
• Disadvantages of Arrays:
• Wasted Memory
– One of the disadvantages of arrays is that memory could
be wasted since memory is allocated statically (at compile
time).