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? COMP26120 Lecture C2 2/28
Lecture Outline Lab 4: Arrays, Strings, Parameters Lab 5: Struct, Union, Enum, Typedef, Pointer & Malloc COMP26120 Lecture C2 3/28
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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