day9 coding: 指针的应用

This commit is contained in:
flykhan 2023-07-13 19:18:49 +08:00
parent ce2aa7304d
commit 1c3315b3c9
13 changed files with 279 additions and 0 deletions

29
day9/d1.c Normal file
View File

@ -0,0 +1,29 @@
// 函数指针
// 应用场景: 函数回调
// 函数指针的表示: int (*函数指针名)(形参类型列表)
#include <stdio.h>
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;
}

27
day9/d10.c Normal file
View File

@ -0,0 +1,27 @@
#include <stdio.h>
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;
}

27
day9/d11.c Normal file
View File

@ -0,0 +1,27 @@
#include <stdio.h>
#include <stdlib.h>
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;
}

44
day9/d12.c Normal file
View File

@ -0,0 +1,44 @@
// 函数的指针数组
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
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;
}

20
day9/d13.c Normal file
View File

@ -0,0 +1,20 @@
// 指针的指针
// 指针变量也是变量,也有自己的内存空间
#include <stdio.h>
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;
}

13
day9/d14.c Normal file
View File

@ -0,0 +1,13 @@
#include <stdio.h>
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;
}

16
day9/d2.c Normal file
View File

@ -0,0 +1,16 @@
#include <stdio.h>
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;
}

17
day9/d3.c Normal file
View File

@ -0,0 +1,17 @@
#include <stdio.h>
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;
}

30
day9/d4.c Normal file
View File

@ -0,0 +1,30 @@
#include <stdio.h>
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;
}

15
day9/d5.c Normal file
View File

@ -0,0 +1,15 @@
#include <stdio.h>
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;
}

13
day9/d6.c Normal file
View File

@ -0,0 +1,13 @@
#include <stdio.h>
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;
}

13
day9/d7.c Normal file
View File

@ -0,0 +1,13 @@
#include <stdio.h>
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;
}

15
day9/d9.c Normal file
View File

@ -0,0 +1,15 @@
#include <stdio.h>
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;
}