Pointers - PowerPoint PPT Presentation

About This Presentation
Title:

Pointers

Description:

Pointers Lecture 1 Thu, Jan 15, 2004 Topics Pointers Declaring Pointers Null Pointers The Address Operator Dereferencing Pointers Pointers to Void Assignment of ... – PowerPoint PPT presentation

Number of Views:143
Avg rating:3.0/5.0
Slides: 11
Provided by: Prefe123
Learn more at: https://people.hsc.edu
Category:
Tags: pointers

less

Transcript and Presenter's Notes

Title: Pointers


1
Pointers
  • Lecture 1
  • Thu, Jan 15, 2004

2
Topics
  • Pointers
  • Declaring Pointers
  • Null Pointers
  • The Address Operator
  • Dereferencing Pointers
  • Pointers to Void
  • Assignment of Pointers

3
Pointers
  • A pointer is an object that refers to another
    object.
  • A pointer holds the address of the object that it
    refers to.
  • All pointers occupy 4 bytes of memory.

4
Declaring Pointers
  • Write the type of object that the pointer points
    to, followed by , followed by the pointer name.

int pi float pf
5
Null Pointers
  • A null pointer is a pointer that has the value
    zero.
  • A null pointer does not point to any object.
  • The constant NULL has the value 0.
  • It is a good practice to initialize a pointer to
    NULL.

int pi NULL
6
The Address Operator
  • The unary prefix operator returns the address
    of an object.
  • The address may be assigned to a pointer.
  • Example AddressOperator.cpp

int i int pi i // i is address of i
7
Dereferencing Pointers
  • When a pointer is dereferenced, it returns the
    object that it points to.
  • The unary pre-fix operator is used to
    dereference a pointer.
  • Never dereference a null pointer.
  • Example DereferencePointers.cpp

int i 10 int pi i // pi points to i cout
ltlt pi ltlt endl // Print i
8
Pointers to void
  • A pointer to void (void object) is a pointer
    that does not point to any particular kind of
    object.
  • (void objects do not exist.)
  • void is treated as a neutral type for
    situations where the type does not matter.
  • A void pointer may hold the address of any
    object.
  • A void pointer may not be dereferenced.

9
Assignment of Pointers
  • The pointer assignment p1 p2 is legal if and
    only if
  • p1 and p2 point to the same type of object, or
  • p1 is a pointer to void.
  • Otherwise, cast p2 to be compatible with p1.
  • Assigning pointers to different types can cause
    serious problems.

10
Example Pointer Assignment
  • Example PtrCompatibility.cpp

void pv int pi float pf pv pi pi
(int)pf pf (float)pv
Write a Comment
User Comments (0)
About PowerShow.com