COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s...
Transcript of COMP40...int main(){char *my_string; int length = get_string(my_string); printf(“We have string %s...
COMP40More on Abstract Data Types
This generalization is known as universal polymorphism
List_push(List_T list, void *v)
void *v = s;
void *v = c;
void *v = p;
void *v = l;
/* Declare the type Table_T to be a struct pointer */typedef struct T *T;
/* Define Table_new to return the struct pointer */T Table_new(...);
#define T Table_T
struct Car {char *brand; int year;}; typedef struct Car Car;
List_T mylist = List_list(NULL); Car *retrieved_car;Car mycar = {"ford", 2000};
mylist = List_push(mylist, &mycar); mylist = List_pop(mylist, (void **)&retrieved_car);
List_free(&mylist);
void double(int n) {n = n * 2;
}
int x = 3;double(x);printf(“x = %d\n”, x);
void double(int *n) {*n = *n * 2;
}
int x = 3;double(&x);printf(“x = %d\n”, x);
int main(){ char *my_string; int length = get_string(my_string); printf(“We have string %s of size %d”, my_string, length);
}
int get_string(char *sp) { sp = malloc(...); /* Set *sp, *(sp+1), … */ return strlen(sp);}
/* Allocate a chunk of memory and read a string into it. Return both a pointer to the string and its length. */
int main(){ char *my_string; int length = get_string(my_string) ; printf(“We have string %s of size %d”, my_string, length);
}
int get_string(char *sp) { sp = malloc(...); /* Set *sp, *(sp+1), … */ return strlen(sp);}
my_string
length = ?
sp
/* Allocate a chunk of memory and read a string into it. Return both a pointer to the string and its length. */
int main(){ char *my_string; int length = get_string(my_string) ; printf(“We have string %s of size %d”, my_string, length);
}
int get_string(char *sp) { sp = malloc(...); /* Set *sp, *(sp+1), … */ return strlen(sp);}
my_string
length = ?
sp
/* Allocate a chunk of memory and read a string into it. Return both a pointer to the string and its length. */
int main(){ char *my_string; int length = get_string(my_string) ; printf(“We have string %s of size %d”, my_string, length);
}
int get_string(char *sp) { sp = malloc(...); /* Set *sp, *(sp+1), … */ return strlen(sp);}
my_string
length = ?
sp
h e l l o \0
/* Allocate a chunk of memory and read a string into it. Return both a pointer to the string and its length. */
int main(){ char *my_string; int length = get_string(my_string) ; printf(“We have string %s of size %d”, my_string, length);
}
int get_string(char *sp) { sp = malloc(...); /* Set *sp, *(sp+1), … */ return strlen(sp);}
my_string
length = ?
sp
h e l l o \0
length = 5
my_string/* Allocate a chunk of memory and read a string into it. Return both a pointer to the string and its length. */
int main(){ char *my_string; int length = get_string( &my_string); printf(“We have string %s of size %d”, my_string, length);
}
int get_string(char **sp_p) { *sp_p = malloc(...); /* Set **sp_p, *(*sp_p + 1), … */ return strlen(*sp_p);}
my_string
length = ?
sp_p
/* Allocate a chunk of memory and read a string into it. Return both a pointer to the string and its length. */
int main(){ char *my_string; int length = get_string( &my_string); printf(“We have string %s of size %d”, my_string, length);
}
int get_string(char **sp_p) { *sp_p = malloc(...); /* Set **sp_p, *(*sp_p + 1), … */ return strlen(*sp_p);}
my_string
length = ?
sp_p
h e l l o \0
length = 5
/* Allocate a chunk of memory and read a string into it. Return both a pointer to the string and its length. */
void print_hi() {printf(“%s\n”, “hi”);
}void print_no() {
printf(“%s\n”, “no”);}void print_yes() {
printf(“%s\n”, “yes”);}
void print_string(char *s) {printf(“%s\n”, s);
}
void List_push_int(List_int *l, int x) { /* Add in new integer element */}void List_push_char(List_char *l, char x) { /* Add in new character element */}
void List_push(List_T *l, void *x) { /* Add in new element */}
int find_min(int[] nums, int size) { int min = nums[0]; for (int i = 1; i < size; i++) { if (min > nums[i]) min = nums[i]; } return min;}
int find_max(int[] nums, int size) { int max = nums[0]; for (int i = 1; i < size; i++) { if (max < nums[i]) max = nums[i]; } return max;}
T f(int[] nums, int size, <func>) { T cl; for (int i = 0; i < size; i++) { cl = <func>(nums[i], cl); } return cl;}
void f(int[] nums, int size, <func>) { for (int i = 0; i < size; i++) { <func>(nums[i]); } }
int x = 77;int *p = &x;
void f() {printf(“Hello”);
} f<func> *fp;fp = f; pfp();
int f(int x); //function declaration (prototype)
int *g(int x); //function returning an int *int (*h)(int x); //function pointer declaration
int f(int x) {return x + 1;
}
int main() {//& and * are optionalh = &f;printf(“%d\n”, (*h)(4));
}
int f(int x); //function declaration (prototype)
int *g(int x); //function returning an int *int (*h)(int x); //function pointer declaration
int f(int x) {return x + 1;
}
int main() {//& and * are optionalh = f;printf(“%d\n”, h(4));
}
int cube(int n) { return n * n * n;}int func_app(int n, int f(int x)) { return f(n);}int main() { int (*func_ptr)(int x); //parameters may be named func_ptr = cube; printf(“%d\n”, cube(4)); printf(“%d\n”, func_ptr(4)); printf(“%d\n”, func_app(4, func_ptr));}
int digits[30];int i;
for (i = 0; i < 30; i++) { digits[i] = i;}
for (i = 0; i < 30; i++) { digits[i] = f(i);}
for (i = 0; i < 30; i += 2) { digits[i] = g(i);}
✓
void f(void **x, void *cl) { //perform computation on *x}
List_T list = List_list(...);
//populate the list
List_map(list, f, NULL);
✓
✓
✓
void f(void **x, void *cl) { (void) cl; int *elem = *x; *elem += 10;}
int *digits = malloc(3 * sizeof(*digits));
for (int i = 0; i < 3; i++) {*(digits + i) = i;
}List_T list = List_list( digits,
(void *)(digits+1), (void *)(digits+2), NULL);
List_map(list, f, NULL);
void f(void **x, void *cl) { int *elem = *x; int *accum_p = cl; *elem += 10; *accum_p += *elem;}
int *digits = malloc(3 * sizeof(*digits));int accum = 0;for (int i = 0; i < 3; i++) {
*(digits + i) = i;}List_T list = List_list( digits,
(void *)(digits+1), (void *)(digits+2), NULL);
List_map(list, f, &accum);
void f(void **x, void *cl) { int *elem = *x; int *accum_p = cl; *elem += 10; *accum_p += *elem;}int accum = 0;List_T list = List_list(...);List_map(list, f, &accum);
0 1 2
0
void f(void **x, void *cl) { int *elem = *x; int *accum_p = cl; *elem += 10; *accum_p += *elem;}int accum = 0;List_T list = List_list(...);List_map(list, f, &accum);
10 1 2
10
void f(void **x, void *cl) { int *elem = *x; int *accum_p = cl; *elem += 10; *accum_p += *elem;}int accum = 0;List_T list = List_list(...);List_map(list, f, &accum);
10 1 2
10
void f(void **x, void *cl) { int *elem = *x; int *accum_p = cl; *elem += 10; *accum_p += *elem;}int accum = 0;List_T list = List_list(...);List_map(list, f, &accum);
10 11 2
21
void f(void **x, void *cl) { int *elem = *x; int *accum_p = cl; *elem += 10; *accum_p += *elem;}int accum = 0;List_T list = List_list(...);List_map(list, f, &accum);
10 11 2
21
void f(void **x, void *cl) { int *elem = *x; int *accum_p = cl; *elem += 10; *accum_p += *elem;}int accum = 0;List_T list = List_list(...);List_map(list, f, &accum);
10 11 12
33
void f(void **x, void *cl) { int *elem = *x; int *accum_p = cl; *elem += 10; *accum_p += *elem;}int accum = 0;List_T list = List_list(...);List_map(list, f, &accum);
10 11 12
33
void f(void **x, void *cl) { int *elem = *x; int *accum_p = cl; *elem += 10; *accum_p += *elem;}
int *digits = malloc(3 * sizeof(*digits));int accum = 0;for (int i = 0; i < 3; i++) {
*(digits + i) = i;}List_T list = List_list( digits,
(void *)(digits+1), (void *)(digits+2), NULL);
List_map(list, f, &accum);
10 11 12
1011
12
10 11 12
1011
12