5 Pointers Function Overloading Pointers and Arrays Pointer

  • Slides: 19
Download presentation
5장 Pointers ㅎㅎ Function Overloading Pointers and Arrays Pointer Arithmetic

5장 Pointers ㅎㅎ Function Overloading Pointers and Arrays Pointer Arithmetic

Function Overloading Two functions have the same name Functions can be distinguished by types

Function Overloading Two functions have the same name Functions can be distinguished by types of function header During compile time we can select the appropriate definition of a overloaded function d = max(2. 0, 3. 0); i = max(2, 3); double max(double num 1, double num 2) { if (num 1 > num 2) return num 1; else return num 2; } int max(int num 1, int num 2) { if (num 1 > num 2) return num 1; else return num 2; } 2

[practice 1] Function Overloading 3

[practice 1] Function Overloading 3

Pointers A pointer is a variable that holds the address of something else. •

Pointers A pointer is a variable that holds the address of something else. • int foo; • int *x; 0 1 2 3 4 5 • 123 • x 81345 81346 81347 • . . . • foo = 123; • x = &foo; • foo • Address • MEMORY • 3 L 6. 4 4

Assigning a value to a dereferenced pointer A pointer must have a value before

Assigning a value to a dereferenced pointer A pointer must have a value before you can dereference it (follow the pointer). • int *x; • *x=3; !!! g n i ! ! h ! yt R n O a R o t R t n • E i o p ’t n s e o • x d • int foo; • int *x; • x = &foo; • *x=3; ne i f s i • this oo f o t s t n i o p 5 • x

[practice 2] first pointer variable [ex 2] 6

[practice 2] first pointer variable [ex 2] 6

[explain 2] first pointer variable [ex 2] 7

[explain 2] first pointer variable [ex 2] 7

[practice 3] initialize a pointer [ex 3] 8

[practice 3] initialize a pointer [ex 3] 8

[explain 3] initialize a pointer [ex 3] 9

[explain 3] initialize a pointer [ex 3] 9

Pointers and Arrays In C there is a strong relationship between the concepts of

Pointers and Arrays In C there is a strong relationship between the concepts of pointers and arrays An array name is basically a const pointer. (A pointer with fixed address) This statement is OK!!! int *x; It assigns x the address of the int a[10]; first elements array a. x = a; 10

Pointers arithmetic Integer math operations can be used with pointers. If you increment a

Pointers arithmetic Integer math operations can be used with pointers. If you increment a pointer, it will be increased by the size of whatever it points to. int *ptr = a; *(ptr+2) *ptr a[0] a[1] a[2] a[3] *(ptr+4) a[4] int a[5]; 11

[practice 4] pointer addition [ex 5] 12

[practice 4] pointer addition [ex 5] 12

[practice 4] pointer addition…continue [ex 5] 13

[practice 4] pointer addition…continue [ex 5] 13

[explain 4] pointer addition [ex 5] 14

[explain 4] pointer addition [ex 5] 14

[practice 5] Arrays of Pointers [ex 4] 15

[practice 5] Arrays of Pointers [ex 4] 15

[explain 5] Arrays of Pointers [ex 4] 16

[explain 5] Arrays of Pointers [ex 4] 16

Exercise 17

Exercise 17

Exercise 18

Exercise 18

Thank you

Thank you