EE 200 Lecture 5: Arrays Steven Bell 18 September 2019 Indexing - - PowerPoint PPT Presentation

ee 200 lecture 5 arrays
SMART_READER_LITE
LIVE PREVIEW

EE 200 Lecture 5: Arrays Steven Bell 18 September 2019 Indexing - - PowerPoint PPT Presentation

EE 200 Lecture 5: Arrays Steven Bell 18 September 2019 Indexing arrays int grape[10] 0 1 2 3 4 5 6 7 8 9 10 11 int* raisin = grape; grape[0] raisin[1] grape[10] int i = 1; *(grape + 1) i[grape]; *(raisin + 1) sizeof The quiz didn't


slide-1
SLIDE 1

EE 200

Steven Bell 18 September 2019

Lecture 5: Arrays

slide-2
SLIDE 2

Indexing arrays

int grape[10] 0 1 2 3 4 5 6 7 8 9 10 11 int* raisin = grape; grape[0] grape[10] *(grape + 1) *(raisin + 1) raisin[1] int i = 1; i[grape];

slide-3
SLIDE 3

sizeof

The quiz didn't cover all the cases:

int howBig(char arr[]) { return sizeof arr; } char pear[100]; sizeof pear[1] sizeof pear char* pear_p; sizeof *pear_p sizeof pear_p howBig(pear[1]) howBig(pear) howBig(pear_p)

slide-4
SLIDE 4

sizeof

The quiz didn't cover all the cases:

int howBig(char arr[]) { return sizeof arr; } char pear[100]; sizeof pear[1] sizeof pear char* pear_p; sizeof *pear_p sizeof pear_p howBig(pear[1]) howBig(pear) howBig(pear_p)

GCC will warn you about this:

slide-5
SLIDE 5

sizeof an array

Unlike Python, MATLAB, Java, etc., C doesn't track how big its arrays are. So once you're outside the unit where it was declared, you have to track the size yourself.

slide-6
SLIDE 6

Dangling pointers

Any pointer whose memory has been deallocated Why is this bad? A) You're reading data that might have changed B) You're writing to memory that might be used for something else C) The behavior is unpredictable D) All of the above

slide-7
SLIDE 7

Dangling pointers

For now, you can avoid dangling pointers by never returning a pointer to a local variable. Once we can dynamically allocate memory, it will get more complex.

slide-8
SLIDE 8

Office hours

I'd like to check in with everyone at some point in the next two weeks I'll send out a spreadsheet; sign up for a time.

slide-9
SLIDE 9

Grading

Classwork 1 grades will be published today Don't worry about build failures for Classwork 3; we're fixing the autograder.

slide-10
SLIDE 10

Make

Use "." to redo an action Use visual mode to make edits to many lines at once: Ctrl+v Shift-i navigate with h/j/k/l make edit Escape now you're back in normal mode Use :s/one/two/g for quick find-and-replace

slide-11
SLIDE 11

Make

Make is a tool for automating compilation

slide-12
SLIDE 12

Homework 5 is hosted on Github

Homework 3 regrades are due by Monday 9/23 at 4:30pm Homework 4 due Monday 9/23 at 4:30pm Homework 5 due Wednesday 9/25 at 4:30pm