Kate Gregory with material from Deitel and Deitel - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Kate Gregory with material from Deitel and Deitel

Description:

A collection of items, all the same type, that can be ... Don't reinvent the wheel. Monday, Jan 20, 2002. Kate Gregory. with material from Deitel and Deitel ... – PowerPoint PPT presentation

Number of Views:162
Avg rating:3.0/5.0
Slides: 26
Provided by: kategr
Category:

less

Transcript and Presenter's Notes

Title: Kate Gregory with material from Deitel and Deitel


1
Week 3
  • Questions from Last Week
  • Hand in Lab 1
  • Arrays
  • Pointers
  • Strings
  • Lab 2

2
(No Transcript)
3
Arrays
  • A collection of items, all the same type, that
    can be treated as a unit or one at a time
  • 10 integers
  • 5 floating point numbers
  • 100 characters

4
Memory arrangement
5
Declaring Arrays
  • int scores10
  • char letters26
  • float ratios5
  • Size is fixed

6
Using Array Elements
  • scoresi
  • cout ltlt letters4
  • ratio2 4.7

7
No bounds checking in C
  • int scores10
  • for (int i0 ilt20 i)
  • scoresi 100
  • Youre the programmer!

8
Bug prevention
  • define MAXSCORES 10
  • int scoresMAXSCORES
  • for (int i0 ilt MAXSCORES i)
  • scoresi 100
  • More readable, less error-prone
  • Cant use a variable when declaring the array

9
Initializers
  • int n 5 1, 2, 3, 4, 5
  • Or just
  • int n 1, 2, 3, 4, 5

10
Passing arrays to functions
  • int total(int a)
  • // ...
  • int numbers 1,2,3,4,5
  • int x total(numbers)

11
Taking arrays in functions
  • int total(int a)
  • int tot 0
  • int limit sizeof(a) / sizeof(int)
  • for (int i0 iltlimit i)
  • tot ai
  • return tot

12
Pointers
  • Address of a variable in memory
  • They have a type
  • int p
  • char s

13
operator
  • (address operator) gets address of operand
  • int y 5
  • int yPtr
  • yPtr y // yPtr gets address of y

14
operator
  • (indirection/dereferencing operator)
  • Gives access to what the pointer points to
  • yptr returns y (because yptr points to y)
  • can be used for assignment
  • yptr 7 // changes y to 7
  • and are inverses
  • They cancel each other out

15
Pointers and Arrays
  • int scores10
  • x scores3
  • x
  • y scores
  • y

16
Pointers and Arrays
  • scores1 2
  • int p scores
  • p
  • p 2
  • Adding is cheaper than multiplying

17
String Array of Characters
  • char word K,a,t,e,\0
  • char word Kate
  • Note that \0 and 0 are different.
  • Null terminated strings

18
Accessing characters
  • cout ltlt word
  • cout ltlt word0
  • word0 L
  • Note single quotes

19
Strings and pointers
  • for (char p word p p)
  • cout ltlt p

20
Arrays of Pointers
  • char suit 4 "Hearts", "Diamonds",
    "Clubs", "Spades"
  • Strings are pointers to the first character
  • char each element of suit is a pointer to a
    char
  • The strings are not actually stored in the array
    suit, only pointers to the strings are stored
  • suit array has a fixed size, but strings can be
    of any size

21
Arrays of Pointers
22
Multi Dimensional Arrays
  • Tables with rows and columns (m by n array)
  • Like matrices specify row, then column

23
Multi Dimensional Arrays
  • Initialization
  • int b22 1, 2 , 3, 4
  • Initializers grouped by row in braces
  • If not enough, unspecified elements set to zero
  • int b22 1 , 3, 4
  • Referencing elements
  • Specify row, then column
  • cout ltlt b01

24
String Library
  • Functions originally from C
  • strlen
  • strcat
  • strcpy
  • strcmp
  • strtok
  • Dont reinvent the wheel

25
For Next class
  • Complete Lab 2
  • Late penalty is TERRIBLE
  • Read chapter 6
Write a Comment
User Comments (0)
About PowerShow.com