Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman - PowerPoint PPT Presentation

About This Presentation
Title:

Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman

Description:

Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman CP 202 Chapter 6 Pointer C uses pointers in three different ways: C uses ... – PowerPoint PPT presentation

Number of Views:1199
Avg rating:3.0/5.0
Slides: 11
Provided by: FahadAl4
Category:

less

Transcript and Presenter's Notes

Title: Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman


1
Problem Solving and Program Design in C (5th
Edition)by Jeri R. Hanly and Elliot B. Koffman
  • CP 202
  • Chapter 6

2
Pointer
  • C uses pointers in three different ways
  • C uses pointers to create dynamic data structures
    -- data structures built up from blocks of memory
    allocated from the heap at run-time.
  • C uses pointers to handle variable parameters
    passed to functions.
  • Pointers in C provide an alternative way to
    access information stored in arrays. Pointer
    techniques are especially valuable when you work
    with strings. There is an intimate link between
    arrays and pointers in C.

3
PointerReference Operator ()
  • The memory of your computer can be imagined as a
    succession of memory cells.
  • As soon as we declare a variable, the amount of
    memory needed is assigned for it at a specific
    location in memory (its memory address).
  • The address that locates a variable within memory
    is what we call a reference to that variable.
  • reference to a variable can be obtained by
    preceding the identifier of a variable with an
    ampersand sign (), known as reference operator.
  • The variable that stores the reference to another
    variable is what we call a pointer.

andy 25 fred andy ted andy
  1. we have assigned the value 25 to andy (a variable
    whose address in memory is 1776).
  2. copied to fred the content of variable andy.
  3. copies to ted the reference of andy.

4
PointerDereference Operator ()
  • Using a pointer we can directly access the value
    stored in the variable which it points to. To do
    this, we simply have to precede the pointer's
    identifier with an asterisk (), which acts as
    dereference operator and that can be literally
    translated to "value pointed by".
  • Notice the difference between the reference and
    dereference operators
  • is the reference operator and can be read as
    "address of
  • is the dereference operator and can be read as
    "value pointed by"

andy 25 ted andy beth ted
(andy) andy
5
PointerDeclaring Variables of Pointer Types
  • Due to the ability of a pointer to directly refer
    to the value that it points to, it becomes
    necessary to specify in its declaration which
    data type a pointer is going point to.
  • Declarationsint number char character
    float greatnumber

6
PointerExample 1
  • // my first pointer
  • include ltiostreamgt
  • using namespace std
  • int main ()
  • int firstvalue, secondvalue
  • int mypointer
  • mypointer firstvalue
  • mypointer 10
  • mypointer secondvalue
  • mypointer 20
  • printf("firstvalue is d\n", firstvalue)
  • printf("secondvalue is d\n", secondvalue)
  • return 0

7
PointerExample 1 (OUTPUT)
  • // my first pointer
  • include ltiostreamgt
  • using namespace std
  • int main ()
  • int firstvalue, secondvalue
  • int mypointer
  • mypointer firstvalue
  • mypointer 10
  • mypointer secondvalue
  • mypointer 20
  • printf("firstvalue is d\n", firstvalue)
  • printf("secondvalue is d\n", secondvalue)
  • return 0

firstvalue is 10 secondvalue is 20
8
Pointer (Example 2)Function with Output Arguments
  • void /-this function returns 2
    results-/calculation(int num, / input
    / int cal1, / output / int cal2) /
    output / cal1 num num cal2 num
    numint main(void) int value / input
    number entered by user / int sum / output
    num num / int multi / output num num
    / printf(Enter a value to analyzegt
    ) scanf(lf, value) calculation(value,
    sum, multi) printf(Sum d, Different
    d, sum, multi) return(0)

9
Pointer (Example 3)
10
Pointer (Example 3)
Write a Comment
User Comments (0)
About PowerShow.com