Presentation is loading. Please wait.

Presentation is loading. Please wait.

Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)

Similar presentations


Presentation on theme: "Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)"— Presentation transcript:

1 Instructor: Alexander Stoytchev http://www.ece.iastate.edu/~alexs/classes/2009_Fall_185/ CprE 185: Intro to Problem Solving (using C)

2 Administrative Stuff HW 10 due this Friday @ 8pm Extra credit HW is due this Sunday @ 8pm There will be labs next week Last lecture is on Monday Next Wednesday we have a review for the final

3 Final Exam Final Exam: Thursday Dec 17: 2:15- 4:15 pm

4 Pointers (part 2) CprE 185: Intro to Problem Solving Iowa State University, Ames, IA Copyright © Alexander Stoytchev

5 Chapter (?!?)

6 What is a pointer?

7 Memory Analogy

8 Pointers: Mailbox Analogy

9 A letter fits comfortably in this box

10 A parcel does not. So, they give you a key …

11 … the key opens a larger mailbox …

12 … the parcel is stored there.

13 This is the pointer to the parcel.

14 What is a pointer to a pointer?

15 Pointer to a Pointer (Analogy)

16 First Key opens #9 Second Key opens the parcel box

17 What is an array of pointers?

18 Array of Pointers (Analogy)

19 Pointer Arithmetic

20 Memory Analogy Address 0 Address 1 Address 2 Address 3 Address 4 Address 5 Address 6

21 Memory Analogy (32 bit architecture) Address 0 Address 4 Address 8 Address 12 Address 16 Address 20 Address 24

22 Memory Analogy (32 bit architecture) Address 0x00 Address 0x04 Address 0x08 Address 0x0C Address 0x10 Address 0x14 Address 0x18 Hexadecimal Address 0x0A Address 0x0D

23 The pointer is incremented by the size of the thing to which it points Address 0x00 Address 0x04 Address 0x08 Address 0x0C Address 0x10 Address 0x14 Address 0x18 Memory for variable i int i; int* ip = &i; ip ip +1 An int pointer is incremented by 4 bytes (i.e., 1*sizeof(int) )

24 The pointer is incremented by the size of the thing to which it points Address 0x00 Address 0x04 Address 0x08 Address 0x0C Address 0x10 Address 0x14 Address 0x18 Memory for variable d double d; double* dp = &d; dp dp +1 An double pointer is incremented by 8 bytes (i.e., 1*sizeof(double) )

25 Basic pointer operations int i = 5; Int* ip = &i; printf("%d\n", *ip); [http://www.eskimo.com/~scs/cclass/notes/sx10a.html]

26 Basic pointer operations *ip = 7; [http://www.eskimo.com/~scs/cclass/notes/sx10a.html]

27 Basic pointer operations int j = 3; ip = &j; [http://www.eskimo.com/~scs/cclass/notes/sx10a.html]

28 Basic pointer operations int *ip2; ip2 = ip; [http://www.eskimo.com/~scs/cclass/notes/sx10a.html]

29 Basic pointer operations ip = &i; [http://www.eskimo.com/~scs/cclass/notes/sx10a.html]

30 Pointers and arrays Is there a difference?

31 Pointers and arrays Array indexing: a[0], a[1], a[2], … int *ip = &a[0]; Pointer indexing: *ip, *(ip+1), *(ip+2), … In general a[i] is “equivalent” to *(ip+i)

32 Pointer Arithmetic ip2 = ip1 + 3; ip2 - ip1 == 3 (true or false?)

33 int *ip; int a[10]; ip = &a[3]; ip2 = ip + 1; [http://www.eskimo.com/~scs/cclass/notes/sx10a.html]

34 autoincrement operator ++ (and its companion, --) Both of these are defined for pointers Array version: a[i++] Pointer version: *ip++

35 Prefix form is defined too Array version: a[++i] preincrement form: *++ip *ip-- and *--ip.

36 Copying an array using pointers int array1[10], array2[10]; int *ip1, *ip2 = &array2[0]; int *ep = &array1[10]; for(ip1 = &array1[0]; ip1 < ep; ip1++) *ip2++ = *ip1; [http://www.eskimo.com/~scs/cclass/notes/sx10a.html]

37 Comparing strings using pointers char *p1 = &str1[0], *p2 = &str2[0]; while(1) { if(*p1 != *p2) return *p1 - *p2; if(*p1 == '\0' || *p2 == '\0') return 0; p1++; p2++; } [http://www.eskimo.com/~scs/cclass/notes/sx10a.html]

38 String copy using pointers char *dp = &dest[0], *sp = &src[0]; while(*sp != '\0') *dp++ = *sp++; *dp = '\0'; [http://www.eskimo.com/~scs/cclass/notes/sx10a.html]

39 Arrays of pointers #include int main() { int* pArray[10]; int a; int b; pArray[0] = &a; pArray[1] = &b; system("pause"); }

40 Pointers to functions // function returning an int // and having two arguments (an int and a char) int fun1(int a, char c); // function returning pointer to an int // and having one int argument int *fun2(int a); // pointer to function returning int // and having two arguments (an int and a char) int (*funp)(int a, char c); // two ways to call the function (*funp)(1,’b’); funp(1,’c’);

41 Example #include int fun1(int a, int b) { printf("fun1\n"); return a+b; } int fun2(int a, int b) { printf("fun2\n"); return a-b; } int main() { // pointer to function returning int and having two arguments: an int and a float int (*funp)(int a, int b); funp = fun1; // take the address of the function and assign it to the function pointer (*funp)(1,2); // call the function using the pointer funp = fun2; // reassign the pointer to point to fun2 funp(1,2); // an alternative way of calling a function using a pointer system("pause"); }

42 Pointers to Structures #include typedef struct node { int value; } node_t; int main() { node_t Node; Node.value = 5; // initialize it to 5 printf("value = %d\n", Node.value); // pointer to the statically allocated struct Node node_t *p = &Node; p->value = 6; // change it to 6 printf("value = %d\n", p->value); }

43 THE END


Download ppt "Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)"

Similar presentations


Ads by Google