Numeric Arrays Page 1 What if we entered

  • Slides: 4
Download presentation
Numeric Arrays Page 1 What if we entered illegal subscripts? For example, for the

Numeric Arrays Page 1 What if we entered illegal subscripts? For example, for the previous code, what if we entered the for loop parameters: for (index = 8; index < 12; index++) printf ("vector[%d] = %d; stored at address %lun", index, vector[index], &vector[index]); (This is illegal because index should not take on any values larger than 9) The (slightly modified) output might appear as: vector[8] = 64; stored at address 41553210 vector[9] = 81; stored at address 41553212 vector[10] = -13107; stored at address 41553214 vector[11] = -15863; stored at address 41553216 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Numeric Arrays Page 2 How can there be a vector[10]? Or vector[11]? ? •

Numeric Arrays Page 2 How can there be a vector[10]? Or vector[11]? ? • vector[10] is nothing more than address 10 * 2 = 20 bytes offset from the base (41553194 + 20 = 41553114) • vector[11] is nothing more than address 11 * 2 = 22 bytes offset from the base (41553194 + 22 = 41553116) The command we issued was: index++ Which increments the contents of index (at the time, 9) by 1 &vector[index] Therefore, the location: Would yield the addresses given above But why is the value stored at, for example, vector[10], -13107 ? ? ? Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Numeric Arrays Page 3 If we were to go to address: We might find

Numeric Arrays Page 3 If we were to go to address: We might find 41553114 4155315 11001101 Neg. &vector[10] 01100110010 + 1 01100110011 which equates to One’s Compliment Two’s Compliment = -(213 + 212 + 29 + 28 + 25 + 24 + 21 + 20) = -(8192 + 4096 + 512 + 256 + 32 + 16 + 2 + 1) = -13, 107 Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft

Page 4 This Concludes The Slides for this Section Choose an Option: Repeat Slides

Page 4 This Concludes The Slides for this Section Choose an Option: Repeat Slides for this Section Go To Next Set of Slides For this Chapter Go To Slide Index For Chapter 4 Go To Slide Index For Chapter 5 Go To Slide Index For Textbook Go To Home Page Data Structures in C for Non-Computer Science Majors Kirs and Pflughoeft