CISY 111 Lecture4 Pointers
CISY 111 Lecture4 Pointers
CISY 111 Structured Programming October 2011 3 CISY 111 Structured Programming October 2011 4
In C++ a we declare a pointer type before In order to request dynamic memory we use
using it. the operator new followed by a data type
Declaration takes the form: specifier.
Data_type * name If a sequence of more than one element is
For example required- the number of elements is placed
Int * nptr within brackets [].
This declares a pointer to an integer value. This returns a pointer to the beginning of the
new block of memory allocated.
CISY 111 Structured Programming October 2011 5 CISY 111 Structured Programming October 2011 6
1
3/7/2012
CISY 111 Structured Programming October 2011 7 CISY 111 Structured Programming October 2011 8
The
first expression should be used to delete A pointer does not store data values.
memory allocated for a single element, It stores the reference to a data values
The second one for memory allocated for arrays The value pointed to by a pointer may be of
of elements. any valid data type.
The value passed as argument to delete must The operations on the data values depend
be either a pointer to a memory block or a purely on its type.
null pointer
To reference the value pointed to by a
In the case of a null pointer, delete produces no
pointer,
effect.
*pointer
For example
*p refers to the value pointed to be p.
CISY 111 Structured Programming October 2011 9 CISY 111 Structured Programming October 2011 10
CISY 111 Structured Programming October 2011 11 CISY 111 Structured Programming October 2011 12
2
3/7/2012
CISY 111 Structured Programming October 2011 13 CISY 111 Structured Programming October 2011 14