Save
COMPUTER SCIENCE
structures and pointers
Save
Share
Learn
Content
Leaderboard
Learn
Created by
Elsa Mary
Visit profile
Cards (35)
Structure
A user-defined data type of C++ to represent a collection of logically related data items, which may be of
different
types, under a
common
name
Syntax to define the
structure
1. struct
structure
_
tag
2.
data_type variable1
;
3.
data_type variable2
;
4.
data_type variableN
;
5.
}
Structure
variable
Variables declared for
storing
the details, can be
initialised
Memory space for structure variable
38
Bytes (
4
+ 20 + 10 + 4)
Defining a
structure
without structure tag
1. struct
2.
int adm
_
no
;
3.
char name
[
20
];
4.
char group
[
10
];
5.
float fee
;
6.
st;
Dot
operator (.)
Connects a
structure
variable and its
element
Accessing elements of a
structure
cin >> st.adm_no;
cin.getline(st.name,20);
st.fee =
2500
;
cout
<< st.group;
If two different structures contain
same
elements
, their
variables
cannot
be
assigned
Nested structure
An element of a structure is a
variable
of another structure
Pointer
A variable that can hold the address of a
memory
location
Syntax to declare pointer variable
data_type *
variable;
Address of operator (&)
Used to get the
address
of a
variable
Indirection or dereference operator (*)
Used to
retrieve
the value pointed to by the
pointer
Static memory allocation
Memory allocation that takes place
before
the execution of the program, due to
variable
declaration statements
Dynamic memory allocation
Memory allocation
during the execution of the program, facilitated by the
new operator
Syntax for dynamic memory allocation
pointer_variable =
new data
_
type
;
Memory leak
Orphaned memory blocks
that are not freed using the
delete
operator
Reasons for memory
leak
Remedy for memory
leak
Operations on pointers
1.
Declaration
of integer pointers
2.
Dynamic memory allocation
and
initialisation
3.
Pointer arithmetic
4. Displaying
pointer
value and
dereferenced
value
5. Reading value using
pointer
Dynamic array
Collection of
memory
locations created during run time using the new
operator
Character pointer
Used
to
reference strings
Accessing elements of a structure using pointer
structure_
pointer-
>
element
_name
Self referential structure
A structure in which one of the elements is a
pointer
to the same
structure
Orphaned memory blocks are
undesirable
and can be avoided by properly de-allocating dynamically allocated memory using
delete
Memory leak can lead to
unfavorable
effects due to
increasing
consumption of memory
Freeing
allocated memory
Using the
delete operator
Nested structure
A
structure
within another
structure
Declaring variables of nested
structure
type
struct time current_
time
, next_
time
;
Dynamic memory allocation operator
new
Representing names of 12 months as an array of strings
char *month
[
12
] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
A
structure
can contain another
structure
as an element
If
'ptr'
is a pointer to the variable 'num', the statement 'num=&ptr;' is
invalid
Swap function using pointers
Swap the values of
two
integer variables using
pointers
Accessing array elements
using
pointers
Accessing
and performing arithmetic operations on
array elements
using pointers