comp26120 algorithms and imperative programming
play

COMP26120: Algorithms and Imperative Programming Lecture C2: C - - PowerPoint PPT Presentation

COMP26120: Algorithms and Imperative Programming Lecture C2: C - Simple Data Structures Pete Jinks School of Computer Science, University of Manchester Autumn 2011 COMP26120 Lecture C2 1/28 Review What have you learnt about C in the lab?


  1. COMP26120: Algorithms and Imperative Programming Lecture C2: C - Simple Data Structures Pete Jinks School of Computer Science, University of Manchester Autumn 2011 COMP26120 Lecture C2 1/28

  2. Review What have you learnt about C in the lab? COMP26120 Lecture C2 2/28

  3. Lecture Outline Lab 4: Arrays, Strings, Parameters Lab 5: Struct, Union, Enum, Typedef, Pointer & Malloc COMP26120 Lecture C2 3/28

  4. Lecture C2: You are here Lab 4: Arrays, Strings, Parameters Lab 5: Struct, Union, Enum, Typedef, Pointer & Malloc COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 4/28

  5. 1-D arrays Moodle theme: Information Representation Moodle: Introduction to Arrays; Arrays Declare fred as an array of 100 characters (0 to 99): char fred [100]; Access: fred[0] or fred[99] or fred[i] etc. No bound-checks: fred[-1] or fred[100] etc. COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 5/28

  6. 1-D arrays Moodle theme: Information Representation Moodle: Introduction to Arrays; Arrays Declare fred as an array of 100 characters (0 to 99): char fred [100]; Access: fred[0] or fred[99] or fred[i] etc. No bound-checks: fred[-1] or fred[100] etc. COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 5/28

  7. 1-D arrays Moodle theme: Information Representation Moodle: Introduction to Arrays; Arrays Declare fred as an array of 100 characters (0 to 99): char fred [100]; Access: fred[0] or fred[99] or fred[i] etc. No bound-checks: fred[-1] or fred[100] etc. COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 5/28

  8. 2-D arrays Moodle: Introduction to Arrays; Multidimensional Arrays Declare fred as a 2-dimensional array of 10 by 10 characters: char fred [10][10]; Access: fred[0][0] or fred[9][9] or fred[i][j] etc. COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 6/28

  9. 2-D arrays Moodle: Introduction to Arrays; Multidimensional Arrays Declare fred as a 2-dimensional array of 10 by 10 characters: char fred [10][10]; Access: fred[0][0] or fred[9][9] or fred[i][j] etc. COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 6/28

  10. Strings Moodle: Strings Declare fred as an initialised string of characters: char fred [] = "hello"; Actually stored with an extra 6th byte = (number) 0 Access: fred[0] or fred[5] or fred[i] etc. printf("%s \ n", fred); Q: What will this do: fred[4]= 0; COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 7/28

  11. Strings Moodle: Strings Declare fred as an initialised string of characters: char fred [] = "hello"; Actually stored with an extra 6th byte = (number) 0 Access: fred[0] or fred[5] or fred[i] etc. printf("%s \ n", fred); Q: What will this do: fred[4]= 0; COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 7/28

  12. Strings Moodle: Strings Declare fred as an initialised string of characters: char fred [] = "hello"; Actually stored with an extra 6th byte = (number) 0 Access: fred[0] or fred[5] or fred[i] etc. printf("%s \ n", fred); Q: What will this do: fred[4]= 0; COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 7/28

  13. Pointers (references) Moodle: Types, Operators, and Expressions → Pointers int i; // i is an integer i = 0; // set the value of i to 0 int *p; // p is a pointer to an integer p = &i; // set p pointing to i *p = 1; // set the value of what p points at (i.e. i) to 1 & : a thing → pointer to that thing * : a pointer to a thing → that thing Q: What does this declare? int * a, b; But what use are pointers? COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 8/28

  14. Pointers (references) Moodle: Types, Operators, and Expressions → Pointers int i; // i is an integer i = 0; // set the value of i to 0 int *p; // p is a pointer to an integer p = &i; // set p pointing to i *p = 1; // set the value of what p points at (i.e. i) to 1 & : a thing → pointer to that thing * : a pointer to a thing → that thing Q: What does this declare? int * a, b; But what use are pointers? COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 8/28

  15. Pointers (references) Moodle: Types, Operators, and Expressions → Pointers int i; // i is an integer i = 0; // set the value of i to 0 int *p; // p is a pointer to an integer p = &i; // set p pointing to i *p = 1; // set the value of what p points at (i.e. i) to 1 & : a thing → pointer to that thing * : a pointer to a thing → that thing Q: What does this declare? int * a, b; But what use are pointers? COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 8/28

  16. Pointers (references) Moodle: Types, Operators, and Expressions → Pointers int i; // i is an integer i = 0; // set the value of i to 0 int *p; // p is a pointer to an integer p = &i; // set p pointing to i *p = 1; // set the value of what p points at (i.e. i) to 1 & : a thing → pointer to that thing * : a pointer to a thing → that thing Q: What does this declare? int * a, b; But what use are pointers? COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 8/28

  17. Pointers (references) Moodle: Types, Operators, and Expressions → Pointers int i; // i is an integer i = 0; // set the value of i to 0 int *p; // p is a pointer to an integer p = &i; // set p pointing to i *p = 1; // set the value of what p points at (i.e. i) to 1 & : a thing → pointer to that thing * : a pointer to a thing → that thing Q: What does this declare? int * a, b; But what use are pointers? COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 8/28

  18. Value Parameters void increment (int i) { i = i + 1; } . . . int j= 0; increment(j); printf("%d \ n", j); Q: What happens? COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 9/28

  19. Reference Parameters void increment (int *i) { *i = *i + 1; } . . . int j= 0; increment(&j); printf("%d \ n", j); Q: What happens? COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 10/28

  20. Array Parameters void increment (int i[]) { i[0] = i[0] + 1; } . . . int j[10]; j[0]= 0; increment(j); printf("%d \ n", j[0]); Q: What happens? COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 11/28

  21. Array Parameters as Pointers void increment (int *i) { i[0] = i[0] + 1; } . . . int j[10]; j[0]= 0; increment(j); printf("%d \ n", j[0]); Q: What happens? COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 12/28

  22. 2-D Arrays as Parameters void increment (int i[][]) { i[0][0] = i[0][0] + 1; } . . . int j[10][10]; j[0][0]= 0; increment(j); printf("%d \ n", j[0][0]); Q: What happens? void increment (int n, int m, int i[n][m]) . . . . . . increment(10, 10, j); COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 13/28

  23. 2-D Arrays as Parameters void increment (int i[][]) { i[0][0] = i[0][0] + 1; } . . . int j[10][10]; j[0][0]= 0; increment(j); printf("%d \ n", j[0][0]); Q: What happens? void increment (int n, int m, int i[n][m]) . . . . . . increment(10, 10, j); COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 13/28

  24. Strings as Pointers Declare fred as a pointer to a literal string of characters: char *fred = "hello"; Q: What will this do: fred[4]= 0; arrays/strings are similar to pointers, but they aren’t identical! char *fred = strdup("hello"); COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 14/28

  25. Strings as Pointers Declare fred as a pointer to a literal string of characters: char *fred = "hello"; Q: What will this do: fred[4]= 0; arrays/strings are similar to pointers, but they aren’t identical! char *fred = strdup("hello"); COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 14/28

  26. Functions used as Parameters Moodle theme: Program Structuring → Call by reference functions e.g. int compare1 (int a, int b) { return a <= b; } int compare2 (int a, int b) { return a >= b; } void sort (int n, int j[n], int(*comp)(int, int)) { ... comp(j[x],j[y]) ... } . . . int j[10]; . . . sort (10, j, &compare1); sort (10, j, compare2); (man qsort) COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 15/28

  27. Functions used as Parameters Moodle theme: Program Structuring → Call by reference functions e.g. int compare1 (int a, int b) { return a <= b; } int compare2 (int a, int b) { return a >= b; } void sort (int n, int j[n], int(*comp)(int, int)) { ... comp(j[x],j[y]) ... } . . . int j[10]; . . . sort (10, j, &compare1); sort (10, j, compare2); (man qsort) COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 15/28

  28. Functions used as Parameters Moodle theme: Program Structuring → Call by reference functions e.g. int compare1 (int a, int b) { return a <= b; } int compare2 (int a, int b) { return a >= b; } void sort (int n, int j[n], int(*comp)(int, int)) { ... comp(j[x],j[y]) ... } . . . int j[10]; . . . sort (10, j, &compare1); sort (10, j, compare2); (man qsort) COMP26120 Lecture C2 Lab 4: Arrays, Strings, Parameters 15/28

  29. Lecture C2: You are here Lab 4: Arrays, Strings, Parameters Lab 5: Struct, Union, Enum, Typedef, Pointer & Malloc COMP26120 Lecture C2 Lab 5: Struct, Union, Enum, Typedef, Pointer & Malloc 16/28

Recommend


More recommend