Title: ObjectOriented Programming Using C Third Edition
1Object-Oriented Programming Using CThird
Edition
- Chapter 5
- Understanding Arrays, Strings, and Pointers
2Objectives
- 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
3Objectives (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
4Understanding 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
5Understanding Memory Addresses (continued)
Address operator
Address appears as a hexadecimal number
6Understanding 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
7Understanding Arrays (continued)
- int someNumbers7
- If you access someNumbers7, you may get a
warning or a garbage value
coutltltsomeNumbers produces the same output as
coutltltsomeNumbers0
8Storing 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
9Accessing and Using Array Values
10Accessing and Using Array Values
- You can also use a variable as a subscript
11Accessing and Using Array Values (continued)
12Avoiding Common Array Errors
- When working with arrays, common errors include
- Forgetting that arrays are zero based
13Pitfall Accessing Locations Beyond the Array
14Using Part of an Array
15Using Part of an Array (continued)
- Tip You cannot use a variable to declare an
arrays size you must use a constant
16Using Parallel Arrays
- Parallel arrays are corresponding arrays in which
values in the same relative locations are
logically related
17Using Parallel Arrays (continued)
18Using Parallel Arrays (continued)
19Using Parallel Arrays (continued)
Flag could also be of type bool
20Creating Arrays of Structure Objects
21Using 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
22Strings 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
23Special 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
24Pitfall Trying to Input a String Value Including
Whitespace
25Pitfall Trying to Input a String Value Including
Whitespace (continued)
26Pitfall 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)
27Pitfall 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)
29Pitfall Exceeding an Arrays Bounds
30Pitfall Exceeding an Arrays Bounds (continued)
31An Introduction to the string Class
32An Introduction to the string Class (continued)
33Using 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
34Using a Pointer Instead of an Array Name
35Using a Pointer Instead of an Array Name
(continued)
36You Do It Using an Array
37Understanding Memory Addresses
38Summary
- 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
39Summary (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
40Summary (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