Announcements - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Announcements

Description:

Tuts: Tut 3 8th ,9th, 15th and 16th September. Labs: Lab 6 22nd , 23rd ,29th and 30th September. Lab 7 6th , 7th , 13th and 14th October ... – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 13
Provided by: robynve
Category:

less

Transcript and Presenter's Notes

Title: Announcements


1
Announcements
  • Tests
  • Test 4 Monday 22th September (ZOO1)
  • Test 5 Monday 13th October (ZOO1)
  • Tuts
  • Tut 3 8th ,9th, 15th and 16th September
  • Labs
  • Lab 6 22nd , 23rd ,29th and 30th September
  • Lab 7 6th , 7th , 13th and 14th October

2
Pointers
  • So far we have used variables in our programs to
    hold information
  • These are locations of a specific size in memory
    where we can store data
  • Each location in our computer has a unique number
    associated with it
  • This is called an address

3
Declaring Variables
  • When we declare a variable we do it as follows
  • int myint
  • We have reserved a space in memory that is large
    enough to hold and integer we have also given
    that space a name myint

4
Pointers
  • It is often useful to refer to a variable
    indirectly
  • We can do this by creating a new data type which
    holds the address of the variable
  • This is called a pointer
  • To create a pointer we do it as follows
  • int mypointer
  • The mypointer variable has been declared as
    holding the address of an integer

5
Pointers
Pointer
6
Example
  • If is say the following
  • the first C example in these notes is on page 1
  • I have given you a pointer to a C example
  • If I have an integer stored at memory address
    0x223F then I can use this number to find that
    integer

7
The Operator
  • The operator is a unary operator
  • This means that it only operates on one item
  • It associates itself with the name to the right
    of it
  • Therefore in the following example pointer is a
    pointer and integer is a normal variable
  • int pointer, integer

8
Address Size
  • On a Pentium based PC all pointers are 32-bits
    long regardless of what they are pointing to
  • This means that a pointer to an integer is the
    same size as a pointer to a char or a float
  • Despite this C treats pointers to different
    variable types as different types of pointers
  • This is because when we write into the location
    pointed to by the pointer, the number of storage
    locations affected will depend on the type of
    pointer

9
The Operator
  • If we have an example and we wish to know where
    it is stored we can use the operator
  • This is illustrated in the following example
  • int myint
  • int intpointer
  • intpointer myint
  • In this example we have declared an integer and
    something which can point to an integer
  • We have then told the pointer to point to the
    integer by assigning the pointer (which holds an
    address) to the address of the integer
  • The unary operator will give us the address of
    the space in memory where the variable is stored

10
Using Pointers
  • If we wish to refer to the thing pointed to by
    the pointer we can do it by prefacing the name of
    the pointer with
  • A full example is shown in the following slide
  • In this example an integer and a pointer to an
    integer are declared
  • After this we make the pointer point to the
    integer
  • We then assign the integer a number and print out
    the contents of the item being pointed to by the
    pointer

11
Example
  • include ltstdio.hgt
  • include ltstdlib.hgt
  • int main(void)
  • int myint
  • int intpointer
  • intpointer myint
  • myint 3456
  • printf(i,intpointer) //This will print
    3456
  • system(PAUSE)

12
Null Pointers
  • When you first create a pointer it does not point
    to anything
  • If you store something in the referenced location
    you have no control over where in memory it is
    stored
  • We are able to create a pointer which does not
    point to anything
  • This is called a Null Pointer
Write a Comment
User Comments (0)
About PowerShow.com