lecture 6 - nus computing - homeooiwt/cs1010/1819s1/slides/cs1010-lec… · lecture 6 admin matters...

61

Upload: others

Post on 18-Oct-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:
Page 2: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

Lecture 6

Admin MattersCommon C MistakesUnit 13: Call Stack

Unit 14: Pointer Unit 15: Array Unit 16: String

18 September 2018

Page 3: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

AY18/19 Sem 1

Midterm

Venue: MPSH 1 (B) 2 October 4pm - 6pm

Page 4: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

AY18/19 Sem 1

Midterm

Open Book Lecture 1 to 5

Page 5: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

AY18/19 Sem 1

Tutorial 5

Problem Sets from Units Today

Page 6: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

AY18/19 Sem 1

Assignment 1

being graded

Page 7: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

AY18/19 Sem 1

Assignment 1

solutions & general comments to be made

available tomorrow

Page 8: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

AY18/19 Sem 1

Assignment 2

Due this Friday 6pm

Page 9: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

AY18/19 Sem 1

Assignment 3

Release this Friday (to be graded on

correctness and style)

Page 10: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

AY18/19 Sem 1

Assignment 3

Everything up to arrays

Page 11: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

AY18/19 Sem 1

Assignment 3

Due 5 October 2018

Page 12: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

AY18/19 Sem 1

Practical Exam 1 Midterm

Information posted online

Page 13: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

AY18/19 Sem 1

Previously on CS1010..

Page 14: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

AY18/19 Sem 1

Unit 5:

We will use long and double only for

CS1010

Page 15: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

AY18/19 Sem 1

Using int will cause your program to fail

on large inputs.

Page 16: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

AY18/19 Sem 1

What’s the advantage of int over long?

(besides memory usage)

Page 17: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

AY18/19 Sem 1

Using float will cause your program to loose precision.

Page 18: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

AY18/19 Sem 1

What’s the advantage of float over double?

(besides memory usage)

Page 19: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

AY18/19 Sem 1

Please follow instructions given in

the assignment.

Page 20: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

AY18/19 Sem 1

e.g., not writing function area_of_rectangle

Page 21: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

AY18/19 Sem 1

e.g., not solving digits

recursively

Page 22: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

long square(long x) { return x*x;}

double hypotenuse_of(long base, long height) { return sqrt(square(base) + square(height));}

double hypotenuse;

int main() { hypotenuse = hypotenuse_of(base, height);}

nota function

Page 23: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

AY18/19 Sem 1

“This is a very very bad programming habit. So, don’t do

this”

Page 24: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

AY18/19 Sem 1

There are still students who do this

😖

Page 25: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

AY18/19 Sem 1

Strict policy on plagiarism

Disciplinary action will be taken :(

Page 26: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

AY18/19 Sem 1

There are still students who do this

😖

Page 27: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:
Page 28: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

AY18/19 Sem 1

Call Stack

Page 29: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

int main()

{

long x = 1;

long y;

}

Page 30: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

int main()

{

long x = 1;

long y;

}

Call Stack

xstack frame 1x

y

Page 31: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

long add(long a, long b) { long sum; sum = a + b; return sum; }

int main() { long x = 1; long y; y = add(x, 10); }

Page 32: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

long add(long a, long b) { long sum; sum = a + b; return sum; }

int main() { long x = 1; long y; y = add(x, 10); } x

1x

y

x

1

10

a

b

11sum

Page 33: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

long add(long a, long b) { long sum; sum = a + b; return sum; }

int main() { long x = 1; long y; y = add(x, 10); } x

1

11

x

y

Page 34: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

long add(long a, long b) { long sum; sum = a + b; a = 42; return sum; }

int main() { long x = 1; long y; y = add(x, 10); }

Page 35: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

long add(long a, long b) { long sum; sum = a + b; a = 42; return sum; }

int main() { long x = 1; long y; y = add(x, 10); }

x

1x

y

x

1

10

a

b

11sum

Page 36: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

long add(long a, long b) { long sum; sum = a + b; a = 42; return sum; }

int main() { long x = 1; long y; y = add(x, 10); }

x

1x

y

x

42

10

a

b

11sum

Page 37: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

long add(long a, long b) { long sum; sum = a + b; a = 42; return sum; }

int main() { long x = 1; long y; y = add(x, 10); }

x

1

11

x

y

Page 38: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

void add(long sum, long a, long b) { sum = a + b; }

int main() { long x = 1; long sum; add(sum, x, 10); }

Page 39: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

void add(long sum, long a, long b) { sum = a + b; }

int main() { long x = 1; long sum; add(sum, x, 10); }

x

1x

sum

Page 40: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

void add(long sum, long a, long b) { sum = a + b; }

int main() { long x = 1; long sum; add(sum, x, 10); }

x

1

??

x

sum

x

1

10

a

b

??sum

Page 41: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

void add(long sum, long a, long b) { sum = a + b; }

int main() { long x = 1; long sum; add(sum, x, 10); }

x

1

??

x

sum

x

1

10

a

b

11sum

Page 42: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

void add(long sum, long a, long b) { sum = a + b; }

int main() { long x = 1; long sum; add(sum, x, 10); }

x

1

??

x

sum

Page 43: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

& address of a variable (i.e, address of a memory location)

* memory location at an address (i.e., the variable at the address)

Page 44: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

& address of a variable

if x is a variable, then &x gives us the address of x.(where does x live?)

Page 45: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

Suppose we have anaddress a, how to findout who lives there?

answer: *a

Page 46: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

x

x

ptr

int main()

{

long x;

long *ptr;

ptr = &x;

*ptr = 1;

}

Page 47: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

#include "cs1010.h" void add(long sum, long a, long b) { sum = a + b; cs1010_println_long((long)&sum); }

int main() { long x = 1; long sum; add(sum, x, 10); cs1010_println_long((long)&sum); }

Page 48: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

Some Rules about Pointers• A pointer to some type T can only points to a

variable of type T

• We cannot change the address of a variable (but can change what a pointer is pointing to)

• We can perform add and subtract on pointers

Page 49: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

AY18/19 Sem 1

Arrays

Page 50: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

AY18/19 Sem 1

array decay

Page 51: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

AY18/19 Sem 1

long a[10];

a is equivalent to

&a[0]

Page 52: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

AY18/19 Sem 1

long a[2] = {0, 1};long b[2] = {0, 1};

if (a == b) { // always false :}

b = a; // not possible

Page 53: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

AY18/19 Sem 1

a[i] = 10; *(a + i) = 10;

x = a[42];x = *(a + 42);

Page 54: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

AY18/19 Sem 1

long max(long list[], long length){ :}

int main(){ long a[10] = { … }; max(a, 10);}

Page 55: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

AY18/19 Sem 1

long max(long *list, long length){ :}

int main(){ long a[10] = { … }; max(a, 10);}

Page 56: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

AY18/19 Sem 1

long a[ ? ];

Page 57: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

AY18/19 Sem 1

long size = cs1010_read_long(); :long a[size];

variable-size array

Page 58: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

AY18/19 Sem 1

long size = cs1010_read_long(); :long *a = cs1010_read_long_array(size);

variable-size array

Page 59: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

AY18/19 Sem 1

Strings

Page 60: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

AY18/19 Sem 1

A string is an array of char

terminated by ‘\0’

Page 61: Lecture 6 - NUS Computing - Homeooiwt/cs1010/1819s1/slides/cs1010-lec… · Lecture 6 Admin Matters Common C Mistakes Unit 13: Call Stack Unit 14: Pointer Unit 15: Array Unit 16:

char *str = “hello!”;

char str[7] = { ‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘!’, ’\0’}