Title: G52CFJ CC for Java Programmers Lecture 3
1G52CFJ C/C for Java Programmers Lecture 3
- Dr Jason Atkin
- E-Mail jaa_at_cs.nott.ac.uk
- Module Convener
- Professor David Brailsford
2Revision so far
- C/C designed for speed, Java for catching
errors - Java hides a lot of the details (so can C)
- C/C programs structure
- int main( int argc, char argv )
- argc is argment count
- argv is an array of (string) command line
arguments - char has been treated as a string so far
- Much of C/C is very similar to Java
- Some differences
- C has no boolean (C does, called bool)
- Ints can be used instead, 0 means false, non-zero
(or 1) means true - C/C types can vary in size across platforms
- C provides a powerful library of functions
- We have seen printf and atoi so far
- You should include the right header file to use
them
3Lecture Outline
- Today only considering
- Local variables
- Pointers
- What they are, how to use them
- Arrays
- Only one-dimensional arrays
4Assume variables are locals
- int main( int argc, char argv )
-
- short s1, s2
- long l1, l2
- char c1,c2,c3,c4
- do something useful
- return
Assume for the examples in this lecture that
all variables are defined as local variables
i.e. variables defined within a function, e.g.
main()
(We will see other ways for declaring variables
later but this is the easiest.)
5Variables size and location
Each variable has A name In your program
only An address Location in memory A size
Number of bytes it takes up A value The
number(s) actually stored
Does it matter 1) Where a variable is stored? 2)
How big a variable is? And can we find out?
6Variables and memory
- C/C let you find out
- Where your variables are in memory
- How big they are
- In Java we dont care
- In C/C we DO care
- I am going to use the kind of table on the right
(in yellow) throughout these examples
Example, local variables short s1, s2 long l1,
l2 char c1,c2,c3,c4
7IMPORTANT WARNINGS
- Addresses in diagrams are for illustration only
- Actual position of data in memory depends upon
- Compiler
- Operating system
- Optimisation turned on?
- For example, you cannot assume
- That local variables will be in adjacent areas in
memory - The ordering of the bytes in a multiple byte data
type - DO NOT RELY ON POSITIONS OF DATA
- UNLESS YOU KNOW IT IS FIXED
- There are some guarantees
- For arrays and structs, we will see later
8Sending a letter
- How does the postman/woman know where to deliver
your letter?
9Sending a letter
- How does the postman/woman know where to deliver
your letter? - You write the address of the recipient on it
10Sending a letter
- How did you find out what address to put on the
letter?
11Sending a letter
- How did you find out what address to put on the
letter? - You probably asked for it in the past, and wrote
it down somewhere - (Or looked it up in a directory)
12Address of
- We can ask for the address of a variable
- And we should write it down somewhere!
- This is like asking where someone lives
- Use the operator in C/C
- E.g. If we have
- long longvalue 345639L
- Then longvalue is the address where the
variable longvalue is stored in memory - Like the address of a person in a street/town
- Now we just have to write it down
13Data type for an address?
- But what type of data is an address?
- i.e. longvalue is of type ???
- Is it a number?
- Is it 2 numbers combined?
- e.g. segmented memory architecture (Win 3.1)
- Is it an int?
- Is it a long?
- Is it a short?
- How are we going to store it?
- Question Any ideas?
14Pointers
- The pointer type is used for storing addresses
- A pointer is an address (in memory) with details
of the type of data to expect there - Remember two things about pointers
- The value of the pointer is an address in memory
- The type of the pointer says what type of data
the program should expect to find at the address - Thats all there is to them
15Declaring a pointer
- is used to denote a pointer
- i.e. a variable which will hold the address of
some other variable - Examples
- char is a pointer to a char
- int is a pointer to an int
- void is a generic pointer, an address of some
data of unknown type (or a generic address) - For example
- short ps // Declare a pointer to a short
- long pl // Declare a pointer to a long
16The concept of a pointer
Conceptually
variable
pointer
345639
pointer points to/at the variable
- You can think of pointers whichever way is easier
for you - As an address in memory and a type
- As a way of pointing to some other data, and a
record of what type of data you think the thing
pointed at is
17Putting and together
- Example
- Create a long variable
- long l 345639L
- Take the address and store it in a long variable
- i.e. in a pointer to a long
- long pl l
Conceptually
Actually (example addresses)
l
pl
345639
pl points to/at l
pls value is the address of l
18Pointers and addresses
- A pointer is like an address in an address book
- You can keep multiple copies of an address
- You can copy the address into another place
- You can change the address
- You can use it to send a letter or visit a friend
19Pointer example
- short s 965
- short ps1 s
- short ps2 ps1
- Q What goes into the red circled parts?
Conceptually
Actually (example addresses)
s
ps1
ps2
20Pointer example
- short s 965
- short ps1 s
- short ps2 ps1
- Q What goes into the red circled parts?
Conceptually
Actually (example addresses)
s
ps1
965
ps2
21Pointer example
- short s 965
- short ps1 s
- short ps2 ps1
- Q What goes into the red circled parts?
Conceptually
Actually (example addresses)
s
ps1
3000
965
ps2
22Pointer example
- short s 965
- short ps1 s
- short ps2 ps1
- So, assigning one pointer to another means
- It points at the same object
- It has the same address stored in it (i.e. the
same value)
Conceptually
Actually (example addresses)
s
ps1
3000
965
ps2
3000
23Sending a letter (again)
- Does the postman/woman need to know the person it
is being delivered to in order to deliver the
letter?
24Sending a letter
- Does the postman/woman need to know the person it
is being delivered to in order to deliver the
letter? - No! It is sufficient that he/she knows where the
recipient lives!
25Weird Avenue
- We can use an address to find someone and do
something to them - We dont need to know who lives there, or what
the house is like, just where it is - E.g. The person who lives in 3 Weird Avenue must
pay this bill - You can make the person pay without knowing them
- Or Give this present to the person at 1 Weird
Avenue - You can give the person a present without knowing
who they are
The buildings on Weird Avenue
No. 4
No. 1
No. 2
No. 3
26Dereferencing pointers
- We can use the thing pointed at, without knowing
what it is - e.g. without knowing which variable it is
- As long as we know what type of thing it is
- Getting the thing pointed at is called
de-referencing the pointer
27Dereferencing operator
- The operator is used to access the thing that
a pointer points at - For example Define a char and char
- char c1 'h'
- char pc2 c1 // pc2 is a pointer to c1
- Ask for the value of the thing pc2 points at
- char c3 pc2 // pc2 is thing pointed at
- Thinking in terms of pointers holding addresses
- pc2 is a char, so is the address of a char
- pc2 is the char pointed at, i.e. c1!
- So, pc2 is (now) another name for c1
28Dereferencing example
- short s1 965
- short ps1 s1
- short ps2 ps1
- short s2 ps2
- What goes into the red circled parts?
- Hint What is ps2?
Conceptually
Actually (example addresses)
s1
ps1
3000
965
ps2
s2
3000
29Dereferencing example
- short s1 965
- short ps1 s1
- short ps2 ps1
- short s2 ps2
- So, we can access (use) the value of s1 without
knowing it is the value of variable s1 (just the
value at address ps2)
Conceptually
Actually (example addresses)
s1
ps1
3000
965
ps2
s2
3000
965
30Dereferencing example
- short s1 965
- short ps1 s1
- short ps2 ps1
- short s2 ps2
- ps1 4 Q What does this do?
Conceptually
Actually (example addresses)
s1
ps1
3000
965
ps2
s2
3000
965
31Dereferencing example
- ps1 4 changes the value pointed at by ps1
- We can change the thing pointed at without
knowing what variable the address actually refers
to (just change the value at this address) - The value of s1 changed without us mentioning s1
Conceptually
Actually (example addresses)
s1
ps1
3000
4
ps2
s2
3000
965
32An introduction to (1D) arrays (They fit well
with pointers)
You now know everything there is to know about
pointers!There is nothing else to them really,
the rest is all about how to use them
33Simple array creation (1)
- Create an uninitialised array
- Add the square brackets at the end of the
variable declaration, with a size inside the
brackets - e.g. array of 4 chars
- char myarray4
- e.g. array of 6 shorts
- short secondarray6
- e.g. array of 12 chars
- char thirdarray12
- Values of the array elements are unknown!
- Whatever was left around in the memory locations
- See sample 3_array_example.c
34Simple array creation (2)
- Creating an initialised array
- You can specify initial values, in
- E.g. 2 shorts, with values 4 and 1
- short shortarray2 4, 1
- E.g. 3 chars, with values o, n and e
- char chararray3 'o','n','e'
- You can let the compiler work out the size
- long longarray (size 3)
- 100000, 5, 543
- char chararray2 (size 8) 'c','','','
c','h','a','r','\0'
35Arrays in memory
- C-Arrays are stored in consecutive addresses in
memory (this is one of the few things that you
CAN assume about data location) - Example
- short s 4,1
- long l 100000,5
- char ac 'c','','','c',
- 'h','a','r','\0'
- Important point From the address of the first
element you can find the addresses of the others
36What we do and dont know
- The addresses of elements in an array are
consecutive - The relative locations of different arrays, or
variables are NOT fixed - Example
- short s 4,1
- long l 100000,5
- char ac 'c','','','c',
- 'h','a','r','\0'
- With a different compiler you may instead get a
different ordering, or gaps
37Accessing an array element
- Exactly the same as in Java, use
- E.g.
- char ac 'c','','','c',
- 'h','a','r','\0'
- char c ac4
- Using what we have seen of pointers
- char pc1 (ac0)
- char pc2 (ac5)
38Java vs C arrays length
- A problem in C/C (not Java)
- char ac 'c','','','c','h','a', 'r','\0'
- char c ac4
- char c2 ac12 OOPS!
- How long is my array?
- Java arrays include a length
- C arrays do not. You could
- Label the last element with unique value?
- Store the length somewhere?
- If you can find the array size, work out the
length
39Java vs C arrays bounds checks
- Java will throw an exception if you try to
read/write beyond the bounds of an array - C/C will let you read/overwrite whatever
happens to be stored in the address if you
read/write outside of array bounds - Checking would take time, speed vs safety
40Arrays and pointers
41Array names act as pointers
- The name of an array is a pointer to the first
element in the array - So, to get a pointer to the first element in the
array we can use - char ac 'c','','','c',
- 'h','a','r','\0'
- char pc3 ac
- i.e. we do NOT need
- char pc3 (ac0)
42You can treat pointers as arrays
- Treating a pointer as an array
- char ac 'c','','','c',
- 'h','a','r','\0'
- char str ac
- char c str4 // c gets value h
- The type of pointer indicates the type of array
- The compiler assumes that you know what you are
doing - i.e. it assumes that the pointer really has the
address of the first element of an array if you
try to use it as such - So if you are wrong, you can break things
43Uninitialised Pointers
- Unless you initialise them, the value of a
pointer is undefined - Always initialise all variables, including
pointers - You can use NULL
- Dereferencing an unitialised pointer has
undefined results - Could crash your program (likely)
- Could crash your computer (less likely)
- Could wipe your hard drive? (unlikely)
44Summary
45Pointers are important
- If you understand pointers, many other things
will make sense - Do not worry if it is not entirely clear now
- We will see MANY more examples
- Pointers are not complex
- Just remember that they just store an address of
something else - And the type of thing that they point at
- I.e. They point to something else
46Arrays
- You can easily create arrays
- Initialised or uninitialised
- Array elements are stored in consecutive areas of
memory - Very useful see next lecture
- No length is stored for an array
- If you need it you need to store it or work it
out - No bounds checking is performed when you use an
array - The compiler trusts you
- So why waste time checking up on you?
47Exercise fill in the value column
- int i1 4
- int i2 745
- int pi1 i1
- int pi2 i2
- int i3 pi1
- char pc (char)pi1
- char c pc
Sizes assume 4 byte ints and 4 byte pointers
48Next lecture
- More pointer examples
- char and C-strings
- More strings
- The power of pointer arithmetic
Next weeks demo and lab session will help with
these