Title: Object Oriented Systems
1Object Oriented Systems
2Back to Pointers with a Vengeance
- Today
- Pointers
- More Pointers
- Structs
- Still more on Pointers
-
3Introduction
- We have already considered the basics of
pointers. - Today we will extend our understanding of what
can be achieved with pointers. - Remember we can point any sort of data we can
store in memory, including other pointers.
4Pointers to Pointers
- C allows pointer types whose objects are
pointersto other pointers. - Or even pointers to pointers to pointers
- The following is a definition of a pointer to an
int object - int PtrPtr1
- Each asterisk in a definition indicates another
level of dereferencing.
5Pointers to Pointers
- Consider the following example
- int num 25
- int Ptr num
- int PtrPtr 0
- PtrPtr Ptr
- cout ltlt (PtrPtr)
6Pointer Power
- The ability to access and change the value of
something whose name is not part of the
assignment statement makes pointer programming
powerful but potentially confusing. - Errors involving pointers are often subtle and
hard to track down. - A simple technique is to draw your own diagrams
this can be so useful even experienced
professional C coders use this technique to
help debug code.
7CONST
- Const creates an immutable constant. For example
- const int myConstant 42
- Constants are useful for parameters are used for.
They are treated differently than standard
variables by the compiler and are hence
programmatically more efficient. - It has an advantage for programmers over the C
pre-processor define command (a cut and
paste). It is more elegant and provides more
sensible error messaging - const double PI 3.14
8Constants and Pointers
- The modifier const can be applied to pointer
declarations. - However where you place this keyword can give the
statement a drastically different meaning - const char Ptr1 ch
- char const Ptr2 ch
- char const Ptr3 ch
Mean the same Thing the pointer is not a
constant, but what its pointing at is.
Here the opposite. The pointer is a constant, the
char is not.
9Constants and Pointers
- We could even have
- const char const Ptr4 ch
- The C syntax can make understanding these
declarations difficult. - Solution read the declaration backwards
replacing with is a pointer to.
10Program using Pointers
- include ltiostreamgt
- using namespace std
- void swap(char Ptr1, char Ptr2)
-
- char c Ptr1
- Ptr1 Ptr2
- Ptr2 c
-
- int main()
-
- char a y
- char b n
- swap(a,b)
- cout ltlt a ltlt b ltlt endl
- return EXIT_SUCCESS
11Pointers as Parameters
- The above program shows that using pointers we
can mimic passing by reference. - To us C programmers this seems as though we are
just replicating what we did using the operator
to pass by reference. - However to a C programmer this is crucial because
C doesnt provide standard reference parameters.
Life is easier for a C coder ?
12Arrays of Pointers
- Because a pointer is just a type of variable
(i.e. one that stores a memory address) it can
often be treated just like you would any other
variable. - Hence you can quite happily created an array of
pointers if you so wish. - int Ptr45
- This code for example would set up an array of
five pointers to integers.
13Pointers to Arrays
- Never confuse an array of pointers with a
pointer to an array! - In C the name of an array is considered to be a
constant pointer. - The name of the array is associated with a
particular memory location, and this association
cannot be changed - In fact it is associated with the location of the
first element in the array.
14Example
int ant5 int dec7 int Ptr1
ant int Ptr2 ant0 int Ptr3
dec int Ptr4 dec4
Equivalent statements
Ptr3 points at dec0
Ptr3 points at dec4
15Whats going on?
- Say we had
- int myArray410,20,30,40
- int ptr myArray
ptr
myArray
10
myArray0
20
myArray0
30
myArray0
40
myArray0
16Command Line Arguments
- To take arguments in from the command line we
used - int main(int argc, char argv)
argv0
argv1
argv2
null
argc
argv
c
j
2
m
i
5
d
m
\0
\0
\0
17Pointer Arithmetic
- There are only two arithmetic operations that you
can perform on pointers addition and
subtraction. - Hence all of the following expressions are valid
- Ptr
- Ptr--
- Ptr2 Ptr1 - 2
- Ptr (x) 4
- Best use an example to explain this so here goes
18Pointer Arithmetic Example
- Say we have a integer pointer Ptr, with a current
value (which of course is a memory address) of
2000. - Now we have the expression Ptr
- Whats the value of Ptr now? You might expect it
be 2001 but it is in fact 2004. - This is because when the pointer is incremented
it moves to the next int held in memory 4 bytes
across.
19In general
- Each time a pointer is incremented it points to
the memory location of the next element of its
base type.
2000
ptr
2004
ptr1
2008
ptr2
2012
ptr3
2016
ptr4
20Pointer Arithmetic Program
- include ltiostreamgt
- using namespace std
- int main()
-
- int squares 1,4,9,16,25,36
- int ptr
- ptr squares
-
- for(int i0 ilt6 i)
-
- cout ltlt (Ptr i) ltlt endl
-
-
21The Indexing Deception
- So what does this mean? It means these are the
same - These are identical apart from the pointer
array in the left hand code is a constant. On
the right it is not.
int array 1,3,5 cout ltlt array0 cout
ltlt array1 cout ltlt array2
int array 1,3,5 cout ltlt (array) cout ltlt
(array1) cout ltlt (array2)
22Character String Processing
- All this is particularly for our good old null
terminated strings (remember them?) - Null terminated strings are of course character
arrays. The name of the string is just a pointer
to the first letter. - We can use pointer arithmetic to manipulate
arrays and thats exactly what the functions in
ltiostreamgt are doing.
23Interchangeability
- The interchangeability of pointer and array
notation is most obvious when you pass char
strings to functions. - Both of the following are possible
implementations of the strlen library function
int strlen(const char s) int i for(i0
si!\0 i) return i
int strlen(const char s) int i for(i0
s !\0 i) return i
24Structures
- A structure is a collection of related data
identified as a single storage unit. - Each element in a structure is called a data
member. - After it has been declared, an instance of that
structure can be created for use in your program. - It is almost an unwritten law in Computer Science
lectures that any examples of structures must,
and I mean must, be boring. Im talking about
stuff like address books or bus timetables.
25Jims Chauvinistic Struct
- struct vital_statistics
-
- char models_name50
- char hair_colour20
- long int age
- int chest
- int waist
- int hips
Here our struct has two char arrays and 4 ints,
but we could have used any data types, including
other structs.
26The DOT operator
- vital_statistics caprice
- strcpy(caprice.modelsName, caprice)
- strcpy(caprice.hairColour, Blonde)
- caprice.age 24
- caprice.chest 36
- caprice.waist 28
- caprice.hips 32
Note the use of the dot operator, sometimes
called the direct membership operator.
integers and other fundamental types can be
directly accessed. We use the DOT OPERATOR.
27You can have lots of instances
- Once you have defined the struct you can create
as many instance as you want - vital_statistics brad
- strcpy( brad.modelsName, Brad Pitt)
- strcpy( brad.hairColour, greying now Jens
left) - brad.age 35
- brad.chest 40
- brad.waist 32
Example for the girls so I dont get in trouble.
28Quick Initialisation
- Similar to the way you can initialise arrays when
they are declared, you can also instantiate an
object and supply all of its members at once - vital_statistics anotherPerson
- whoever,
- ginger,
- 87,
- 32,
- 24,
- 34
-
You have to remember what order your data
elements were declared
29Whats happening in memory?
modelsName (50 Bytes)
hairColour (20 Bytes)
age (32 Bytes)
chest (16 Bytes)
waist (16 Bytes)
hips (16 Bytes)
30Manipulating Structures
- Accessing Structure Members
- cout ltlt caprice.age
- Assigning to Structure Members
- caprice.age 24
- Assigning whole structures
- vital_statistics a,b
- a.age 24
- b a
Assigning one struct to the other.
31Arrays of Structures
- Perhaps the most common use of structures is in
arrays of structures. To declare an array of
structures, you must first define a structure,
and then declare an array variable of that type - vital_statistics top_trumps52
- To access the structs variables simply index the
the structure name as you would with a normal
array - some_string top_trumps7.name
32Complex Structures
- Members of structures can either be of type
- simple or compound
- Code fragment Example of a struct with compound
members - struct phone_book
- char address50
- int number
- vital_statistics info
-
Any of the built in data types such as an integer
or a character
Arrays of these fundamentals, or other structures
themselves.
33Pointers to structs
- The following code sets up the struct in normal
memory - vital_statistics person
- vital_statistics ptrPerson person
-
-
ptrPerson
?
person
struct
stack
34Accessing via the pointer
- It is easy to get to the fields using the dot
operator but how do you do it via the pointer? - Easy just dereference the pointer using the and
use the dot operator as follows - vital_statistics jim
- vital_statistics ptr jim
- (ptr).modelName James Goulding
- (ptr).hairColour Brown
- (ptr).age 28
35A better method -gt
- So this is fundamentally what is happening you
dereference the pointer and access the fields. - But it all starts to look a bit clumsy.
- C provides a shortcut the -gt operator
- vitalStats jim
- vitalStats ptr jim
- ptr-gtmodelName James Goulding
- ptr-gthairColour Brown
- ptr-gtage 28
36So what is the point?
- Well we have
- an alternative notation for manipulating arrays
- a way to access command line arguments
- A method of pointing to any data type, even
structs - is that it?
- No Sireee. The main role of pointers is for
creating dynamic objects. - Dynamic Memory allows to solves lots of problems
that might otherwise be intractable
37Problems with Pointers
- Wild pointers are the devils work.
- Pointers give you tremendous power, but if one
accidentally contains the wrong value they can be
a bugger to find. - Take as much care as you possibly can not to make
pointer errors. - Classic example is the uninitialised pointer.
38Next Time
- Weve pretty much covered the fundamentals of
pointersnow youve just got to learn how to
apply them. - Next Lecture well be looking at Dynamic Memory
Allocation the real role of pointers. - This will be a graduation from Basic to
Intermediate C.