ObjectOriented Programming Using C Third Edition - PowerPoint PPT Presentation

1 / 40
About This Presentation
Title:

ObjectOriented Programming Using C Third Edition

Description:

Learn techniques to access part of an array. Object-Oriented Programming ... within double quotes ... expressed within double quotes is commonly called ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 41
Provided by: hseK
Category:

less

Transcript and Presenter's Notes

Title: ObjectOriented Programming Using C Third Edition


1
Object-Oriented Programming Using CThird
Edition
  • Chapter 5
  • Understanding Arrays, Strings, and Pointers

2
Objectives
  • Learn about memory addresses
  • Learn about arrays
  • Store values in an array
  • Access and use array values
  • Avoid common errors with arrays
  • Learn techniques to access part of an array

3
Objectives (continued)
  • Use parallel arrays
  • Use arrays of objects
  • Learn how to use strings
  • Discover special string-handling problems
  • Learn about pointers
  • Use a pointer in place of an array name

4
Understanding Memory Addresses
  • Each location where a piece of data can be stored
    is identified by a memory address
  • When you declare a variable with a statement such
    as int myAge
  • The computer chooses an available memory location
  • Associates the name myAge with the memory address
    of that location

5
Understanding Memory Addresses (continued)
Address operator
Address appears as a hexadecimal number
6
Understanding Arrays
  • Array list of items that all have the same type
  • Subscript number that indicates the position of
    the particular array element being used
  • Element single object in an array
  • double moneyCollected5

Reserves sizeof(int)5 bytes of memory
7
Understanding Arrays (continued)
  • int someNumbers7
  • If you access someNumbers7, you may get a
    warning or a garbage value

coutltltsomeNumbers produces the same output as
coutltltsomeNumbers0
8
Storing Values in an Array
  • int rent4
  • rent0 250
  • rent1 375
  • rent2 460
  • rent3 600
  • int rent4 250, 375, 460, 600
  • int rent 250, 375, 460, 600
  • int rent4 250, 375
  • int rent3 250, 375, 460, 600
  • int rent4 0

rent2 and rent3 are set to 0
Syntax error
Sets all array elements to 0
9
Accessing and Using Array Values
10
Accessing and Using Array Values
  • You can also use a variable as a subscript

11
Accessing and Using Array Values (continued)
12
Avoiding Common Array Errors
  • When working with arrays, common errors include
  • Forgetting that arrays are zero based

13
Pitfall Accessing Locations Beyond the Array
14
Using Part of an Array
15
Using Part of an Array (continued)
  • Tip You cannot use a variable to declare an
    arrays size you must use a constant

16
Using Parallel Arrays
  • Parallel arrays are corresponding arrays in which
    values in the same relative locations are
    logically related

17
Using Parallel Arrays (continued)
18
Using Parallel Arrays (continued)
19
Using Parallel Arrays (continued)
Flag could also be of type bool
20
Creating Arrays of Structure Objects
21
Using Strings
  • String value expressed within double quotes
  • You can type two characters within single quotes
    when they represent a single character \n
  • Hello is a string constant
  • To store a value such as Hello, you must create
    a string variable in one of two ways
  • Create a string as an array of characters
  • Create a string using the string class defined in
    the C standard library

22
Strings Created as Arrays of Characters
  • char firstName Mary
  • char firstName Mary
  • char firstName5 Mary
  • char firstName5 Mary
  • char firstName5 'M', 'a', 'r', 'y', '\0'
  • coutltltfirstName // displays Mary
  • coutltltfirstName1 // displays ary

Null character. You could also use the constant
NULL, defined in iostream
23
Special String-Handling Problems When Using
Character Arrays
  • Some problems occur when strings are declared as
    character arrays and you
  • Try to input a string value that includes
    whitespace
  • Try to assign one string to another using the
    assignment operator
  • Try to compare strings using the comparison
    operator
  • Exceed the bounds of an array

24
Pitfall Trying to Input a String Value Including
Whitespace
25
Pitfall Trying to Input a String Value Including
Whitespace (continued)
26
Pitfall Trying to Assign One String to Another
Using the Assignment Operator
  • char clubPresident10 Eric
  • clubVicePresident10 Danielle
  • Alternative 1 (does not work as expected)
  • clubPresident clubVicePresident
  • Alternative 2 (tedious)
  • clubPresident0 clubVicePresident0
  • clubPresident1 clubVicePresident1
  • clubPresident2 clubVicePresident2
  • Alternative 3 (must include string.h)
  • strcpy(clubPresident, clubVicePresident)

27
Pitfall Trying to Compare Strings Using the
Comparison Operator
  • Alternative 1 (does not work as expected)
  • if(clubPresident clubVicePresident)
  • coutltltThey are the sameltltendl
  • Alternative 2 (tedious)
  • if(clubPresident0 clubVicePresident0
    clubPresident1 clubVicePresident1. . .
  • Alternative 3 (must include string.h)
  • strcmp(firstName, secName)

A return value of 0 indicates they are equal
28
(No Transcript)
29
Pitfall Exceeding an Arrays Bounds
30
Pitfall Exceeding an Arrays Bounds (continued)
31
An Introduction to the string Class
32
An Introduction to the string Class (continued)
33
Using Pointers
  • Pointers variables that can hold memory
    addresses
  • A pointers type indicates the type of variable
    that has an address the pointer can hold
  • For example,
  • int aPointer
  • int myValue
  • aPointer myValue
  • coutltltmyValue //outputs contents of myValue
  • coutltltaPointer //outputs contents of myValue
  • coutltltmyValue //outputs address of myValue
  • coutltltaPointer //outputs address of myValue

34
Using a Pointer Instead of an Array Name
35
Using a Pointer Instead of an Array Name
(continued)
36
You Do It Using an Array
37
Understanding Memory Addresses
38
Summary
  • Each location where a piece of data can be stored
    is identified by a memory address
  • A list of individual items that all have the same
    type is called an array
  • A subscript indicates the position of an array
    element
  • You can initialize an array when you declare it
  • You can access an individual array value just as
    you would access any variable of the same type

39
Summary (continued)
  • Common errors when using arrays include
  • Forgetting that arrays are zero-based
  • Forgetting that the highest legitimate subscript
    is one less than the array size
  • Use a for loop to access all array elements
  • Parallel arrays contain corresponding elements
  • You can create arrays of structure objects

40
Summary (continued)
  • A C value expressed within double quotes is
    commonly called a string
  • You can create a string variable
  • As an array of characters
  • With the string class from the C standard
    library
  • Pointers are variables that hold memory addresses
  • To indicate a pointer, begin the variables name
    with an asterisk
Write a Comment
User Comments (0)
About PowerShow.com