CS304: Lecture 4 - PowerPoint PPT Presentation

1 / 11
About This Presentation
Title:

CS304: Lecture 4

Description:

Pointers are types that store memory addresses #include iostream using ... This will become a downfall later. A Quick Lesson in Pointer Arithmetic. int x[10] ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 12
Provided by: faizan
Category:

less

Transcript and Presenter's Notes

Title: CS304: Lecture 4


1
CS304 Lecture 4
  • Pointers and Pointer-Based Strings
  • Deitel, Ch. 8 (part 1)
  • http//www.cis.uab.edu/cs304

2
What are pointers?
  • Pointers are types that store memory addresses
  • include ltiostreamgtusing namespace stdint
    main() int x, y, ptr 0 // ptr is
    a pointer ptr x // x address of
    x cout ltlt ptr ltlt , ptr y
    cout ltlt ptr ltlt endl return 0
  • Output 0xffbef44c, 0xffbef448 // 0x means
    hexadecimal

3
Case Study
  • Please take some time to go through the Case
    Study on Card Shuffling and Dealing Simulation.
    Its rich with concepts explored today.

4
Potential Points of Confusion
  • Using in the declaration of a pointer is NOT an
    operator
  • It is possible to have a interpreted as a unary
    (next slide) or binary operator, but only when
    not involved in a declaration.
  • If uninitialized, pointers should be set to zero
    or NULL (defined in iostream)

5
Pointer Operations
  • The address operator (or address-of
    operator)
  • Takes any variable and provides a pointer-value
    for that variable
  • int ptr variable
  • The indirection operator (or dereferencing
    operator)
  • Takes a pointer variable and provides a synonym
    name, if you prefer to view it that way.
  • After the above, (ptr variable), and cout ltlt
    ptr outputs the value of variable
  • x somevalue is legal.

6
Pointers and Pass-By-Reference
  • There are two types of pass-by-reference
    pass-by-reference with reference arguments, and
    pass-by-reference with pointer arguments.
    (Confused yet?)
  • One can declare a function that takes a pointer
    as an argument, like so
  • void cubeit(int n) n n n n
    //Ewww
  • Hint Parentheses clear things up
  • void cubeit(int n) n (n) (n) (n)

7
Food for thought
  • Passing an array by reference?
  • An array declaration is really a const pointer to
    the beginning of a block of memory, so it is
    automatically passed by reference.

8
Obvious Question References vs. Pointers
  • This isnt important yet, but
  • Pointers were a C feature, References are a C
    feature (as well as Java)
  • Some of the other features of C (that we
    havent learned about yet) do not quite work as
    well with pointers as with references
  • References look prettier!
  • However, references are constant. This will
    become a downfall later.

9
A Quick Lesson in Pointer Arithmetic
  • int x10//Note that x is a pointer
  • x1 (x1) //both access the next data
    //element in the array
  • int y x
  • y y x1
  • y y x2
  • y - 3 y x - 1, unfortunately.

10
Types of Const for Pointers
  • Non-constant pointer to non-constant data
  • char a
  • Non-constant pointer to constant data
  • const char a
  • Constant pointer to non-constant data (Array)
  • char const a
  • Constant pointer to constant data
  • const char const arr

Non-const Mutable !!
11
Interview Question
charconst a char b a
b //? ba //?
const char a char b a
b //? ba //?
X
X
0xffbef44c
20
0xffbef44c
20
y
y
0xffbef448
10
0xffbef448
10
Write a Comment
User Comments (0)
About PowerShow.com