From 1c3315b3c99e4d4d54c208cd5d4ffaff37359e1c Mon Sep 17 00:00:00 2001 From: flykhan Date: Thu, 13 Jul 2023 19:18:49 +0800 Subject: [PATCH] =?UTF-8?q?day9=20coding:=20=E6=8C=87=E9=92=88=E7=9A=84?= =?UTF-8?q?=E5=BA=94=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day9/d1.c | 29 +++++++++++++++++++++++++++++ day9/d10.c | 27 +++++++++++++++++++++++++++ day9/d11.c | 27 +++++++++++++++++++++++++++ day9/d12.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ day9/d13.c | 20 ++++++++++++++++++++ day9/d14.c | 13 +++++++++++++ day9/d2.c | 16 ++++++++++++++++ day9/d3.c | 17 +++++++++++++++++ day9/d4.c | 30 ++++++++++++++++++++++++++++++ day9/d5.c | 15 +++++++++++++++ day9/d6.c | 13 +++++++++++++ day9/d7.c | 13 +++++++++++++ day9/d9.c | 15 +++++++++++++++ 13 files changed, 279 insertions(+) create mode 100644 day9/d1.c create mode 100644 day9/d10.c create mode 100644 day9/d11.c create mode 100644 day9/d12.c create mode 100644 day9/d13.c create mode 100644 day9/d14.c create mode 100644 day9/d2.c create mode 100644 day9/d3.c create mode 100644 day9/d4.c create mode 100644 day9/d5.c create mode 100644 day9/d6.c create mode 100644 day9/d7.c create mode 100644 day9/d9.c diff --git a/day9/d1.c b/day9/d1.c new file mode 100644 index 0000000..41513b9 --- /dev/null +++ b/day9/d1.c @@ -0,0 +1,29 @@ +// 函数指针 +// 应用场景: 函数回调 +// 函数指针的表示: int (*函数指针名)(形参类型列表) +#include + +void callback(int item, int index) +{ + printf("%d->%d\n", index, item); +} + +void callback2(int item, int index) +{ + printf("item %d->%d\n", index, item); +} + +// void (*func)(int, int) 表示函数指针,形参的函数名为 func +void forEach(int a[], int size, void (*func)(int, int)) +{ + for (int i = 0; i < size; i++) + func(a[i], i); // 回调函数 +} + +int main(int argc, char const *argv[]) +{ + int arr[] = {3, 2, 1, 5, 4}; + int size = sizeof(arr) / sizeof(arr[0]); + forEach(arr, size, callback2); // callback2 将存储此函数的空间地址给 func + return 0; +} \ No newline at end of file diff --git a/day9/d10.c b/day9/d10.c new file mode 100644 index 0000000..477ecc2 --- /dev/null +++ b/day9/d10.c @@ -0,0 +1,27 @@ +#include + +int main() +{ + int *arr[10]; // arr 数组用来存储地址,每个元素都是 int * + int a = 1, b = 2; + arr[0] = &a; // 将 a 的地址存放在 arr 数组的第一个元素中 + arr[1] = &b; + + a = a + b; + printf("arr[0] = %d\n", *arr[0]); // *arr[0] = 3 + + int arr2[3] = {10, 20, 30}; + // 将 arr2 数组的第二个元素的地址存放在 arr 数组的第三个元素中 + arr[2] = &arr2[1]; + printf("arr[2] = %d\n", *arr[2]); // *arr[2] = 20 + + int *p = arr[2]; // arr[2] => int * => &arr2[1] + *p = 100; + + for (int i = 0; i < 3; i++) + { + printf("%d ", arr2[i]); // 10 100 30 + // printf("%d ", *(arr2 + i)); // 10 100 30 + } + return 0; +} \ No newline at end of file diff --git a/day9/d11.c b/day9/d11.c new file mode 100644 index 0000000..dd4cfe1 --- /dev/null +++ b/day9/d11.c @@ -0,0 +1,27 @@ +#include +#include + +int main() +{ + char *names[5]; // names 数组用来存储地址,每个元素都是 char *, 等同于 5 个字符串 + for (int i = 0; i < 5; i++) + { + printf("请输入第 %d 个姓名: ", i + 1); + names[i] = (char *)malloc(32); // 为什么需要申请内存?因为 names[i] 是 char * 类型,需要存储字符串,所以需要申请内存,否则会出现段错误,因为 names[i] 指向的内存是不可写的 // names + i => &names[i] + scanf("%s", names[i]); // names + i => &names[i] + } + for (int i = 0; i < 5; i++) + { + printf("%s\n", *(names + i)); + } + printf("%p\n", names[0]); + printf("%s\n", names[0]); + for (int i = 0; i < 5; i++) + { + free(names[i]); + } + printf("%p\n", names[0]); + printf("%s\n", names[0]); // 内存释放后,地址不变,但是地址中的内容清空了 + + return 0; +} \ No newline at end of file diff --git a/day9/d12.c b/day9/d12.c new file mode 100644 index 0000000..ef95f77 --- /dev/null +++ b/day9/d12.c @@ -0,0 +1,44 @@ +// 函数的指针数组 +#include +#include +#include + +void fa(int a, int b) +{ + printf("%d + %d = %d\n", a, b, a + b); +} + +void fb(int a, int b) +{ + printf("%d - %d = %d\n", a, b, a - b); +} + +void fc(int a, int b) +{ + printf("%d * %d = %d\n", a, b, a * b); +} + +void fd(int a, int b) +{ + printf("%d %% %d = %d\n", a, b, a % b); +} + +int main() +{ + // 创建一个函数指针数组 + // 函数声明: int func(int, int); + void (*funcs[4])(int, int) = {fa, fb}; + funcs[2] = fc; + funcs[3] = fd; + + srand(time(NULL)); + for (int i = 0; i < 4; i++) + { + int x, y; + x = rand() % 100; + y = rand() % 100; + funcs[i](x, y); + } + + return 0; +} \ No newline at end of file diff --git a/day9/d13.c b/day9/d13.c new file mode 100644 index 0000000..d3b60e3 --- /dev/null +++ b/day9/d13.c @@ -0,0 +1,20 @@ +// 指针的指针 +// 指针变量也是变量,也有自己的内存空间 +#include + +int main() +{ + int a = 20; + int *p = &a; + + *p = 50; + + int **q = &p; // q 指向 int ** 类型 + + **q += 100; + + printf("p = %p, *q = %p\n", p, *q); // p = 0x7ffeeb0b0a3c, *q = 0x7ffeeb0b0a3c + printf("a = %d, *p = %d, **q = %d\n", a, *p, **q); // a = 50, *p = 50, **q = 50 + + return 0; +} \ No newline at end of file diff --git a/day9/d14.c b/day9/d14.c new file mode 100644 index 0000000..470cbec --- /dev/null +++ b/day9/d14.c @@ -0,0 +1,13 @@ +#include + +int main() +{ + char name1[128] = "hello"; + // char *name2 = "world"; + // printf("%s , %p\n", name2, name2); + // name2 = "world2"; + // printf("%s , %p\n", name2, name2); + // name2 = "world3"; + // printf("%s , %p\n", name2, name2); + return 0; +} \ No newline at end of file diff --git a/day9/d2.c b/day9/d2.c new file mode 100644 index 0000000..776c236 --- /dev/null +++ b/day9/d2.c @@ -0,0 +1,16 @@ +#include + +int main() +{ + int a = 10, b = 20; + int *p = &a, *q = &b; + + a += b++; + + printf("*p = %d\n", *p); + printf("*q = %d\n", *q); + + *q += *p; + printf("*q = %d, *p = %d\n", *q, *p); + return 0; +} \ No newline at end of file diff --git a/day9/d3.c b/day9/d3.c new file mode 100644 index 0000000..b307600 --- /dev/null +++ b/day9/d3.c @@ -0,0 +1,17 @@ +#include + +int main() +{ + char m = 'a'; // 97 + short n = 127 + 129 + 97; + int x = 10; + void *p; + p = &m; + p = &n; + + // (char *) 表示将 p 强制转换为 char 类型的指针,因为 void * 不能直接解引用, 他不能确定类型大小 + printf("p->char, *p=%c\n", *(char *)p); + printf("p->short, *p=%d\n", *(char *)p); + + return 0; +} \ No newline at end of file diff --git a/day9/d4.c b/day9/d4.c new file mode 100644 index 0000000..c474864 --- /dev/null +++ b/day9/d4.c @@ -0,0 +1,30 @@ +#include + +int main() +{ + int arr[10] = {4, 2, 1, 5, 6, 7, 8, 9, 10, 3}; + int *p = arr; + int *q = NULL; // 创建一个空指针,用于存放最小值的地址 + while (p < &arr[9]) + { + q = p + 1; + while (q <= &arr[9]) // 找出本轮最小值 + { + if (*p > *q) + { + *p = *p ^ *q; + *q = *p ^ *q; + *p = *p ^ *q; + // printf("---> %d vs %d\n", *p, *q); + } + q++; + } + p++; // 指向下一个元素 + } + for (int i = 0; i < 10; i++) + { + printf("%d ", arr[i]); + } + printf("\n"); + return 0; +} \ No newline at end of file diff --git a/day9/d5.c b/day9/d5.c new file mode 100644 index 0000000..72ef3e8 --- /dev/null +++ b/day9/d5.c @@ -0,0 +1,15 @@ +#include + +int main() +{ + char arr[] = "abcedf"; + char *p = &arr[0]; // 指向数组的第一个元素的内存地址 + printf("arr[0]=%c\n", *p); + p++; // 指向数组的第二个元素的内存地址 + printf("*p=%c\n", *p); + do + { + printf("*p=%c\n", *++p); + } while (*p != '\0'); + return 0; +} \ No newline at end of file diff --git a/day9/d6.c b/day9/d6.c new file mode 100644 index 0000000..a4ee0c7 --- /dev/null +++ b/day9/d6.c @@ -0,0 +1,13 @@ +#include + +int main() +{ + int arr[] = {1, 2, 3, 4, 6}; + int *p = arr; + + for (int i = 0; i < 5; i++) + { + printf("arr[%d] = %d\n", i, *p++); + } + return 0; +} \ No newline at end of file diff --git a/day9/d7.c b/day9/d7.c new file mode 100644 index 0000000..6fb33ab --- /dev/null +++ b/day9/d7.c @@ -0,0 +1,13 @@ +#include + +int main() +{ + int arr[10] = {1, 2, 3, 4, 5, 6}; + int *p = arr; + + int *q = p + 2; // 指向 arr 数组的第三个元素地址 + int x = *q + 100; // *(p + 2) + 100 + + printf("x=%d\n", x); // 103 + return 0; +} \ No newline at end of file diff --git a/day9/d9.c b/day9/d9.c new file mode 100644 index 0000000..f3477b6 --- /dev/null +++ b/day9/d9.c @@ -0,0 +1,15 @@ +#include + +int main() +{ + int arr[10]; + for (int i = 0; i < 10; i++) + { + printf("%d addr %p\n", i, &arr[i]); + } + int *p = arr, *q = p + 2; + int n = q - p; // 计算两个指针之间的元素个数 + printf("%p - %p = %d\n", q, p, n); + + return 0; +} \ No newline at end of file